3

I am new to stackoverflow and experimenting with Python, currently just trying tutorial examples. Experienced a wonderful learning curve but got completely stuck with the following (working under windows 10):

import shelve
s = shelve.open("test")
Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python36_64\lib\dbm\dumb.py", line 82, in _create
    f = _io.open(self._datfile, 'r', encoding="Latin-1")
FileNotFoundError: [Errno 2] No such file or directory: 'test.dat'

It would be great to get some help to resolve this.

During handling of the above exception, another exception occurred:

ShapeOfMatter
  • 991
  • 6
  • 25

3 Answers3

2

In Python 3, by default, shelve.open tries to open an existing shelf for reading. You have to pass an explicit flag to create a new shelf if if doesn't already exist.

s = shelve.open("test", "c")

This is in contrast to Python 2, where the default flag was "c" instead of "r".

chepner
  • 497,756
  • 71
  • 530
  • 681
-1

How to read an error message

In general, error messages will do their best to tell you what's wrong. In the case of python, you'll typically start at the bottom; here

No such file or directory: 'test.dat'

tells you exactly why the error's being thrown: test.dat doesn't exist.

Next you would read upward through the stack trace until we got to something that we either understood or had written recently, and we'd try to make sense of the error message from there.

How to troubleshoot an error

  • Is the stated problem intelligible?
    • Yes, we asked the software to do something with a (.dat?) file called "test", so we at least know what the hell the error message is talking about.
  • Do we agree with the underlying premise of the error?
    • Specifically, does it make sense that it should matter if test.dat exists or not? Chepner covers this.
  • Do we agree with the specific problem as stated?
    • For example, it wouldn't be weird at all to get such an error message when there was in fact such a file. Then we would have a more specific question: "Why can't the software find the file?" That's progress.
    • (Usually the answer would be either "Because it's looking in the wrong place" or "Because it doesn't have permission to access that file".)
  • Read the documentation for the tools and functions in question.
  • How can we validate either our own understanding of the situation, or the situation described in the error message?
    • Depending on the context this may involve some trial and error of re-writing our code to
      • print out (log) its state during execution
      • do something similar but different from what it was doing, which we're more certain should work.
      • do something similar but different from what it was doing, which we're more certain should not work.
  • Ask for help.
ShapeOfMatter
  • 991
  • 6
  • 25
-1

Seems shelve sometimes uses the dumbdbm to serialize.

Use dbm to use dbm instead:

import dbm
with dbm.open($filename, 'n') as db:
  # read/write
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135