newest is just the filename (e.g. test.db). Since the current directory (by default the directory from which the script was run) is not the same as the db folder, you need to form a full path. You can do that with os.path.join:
data = shelve.open(os.path.join(path,newest), flag = "w")
As Geoff Gerrietts points out, max(filenames) returns the filename that comes last in alphabetical order. Perhaps that does give you the file you desire. But if you want the file with the most recent modification time, then you could use
filenames = [os.path.join(path,name) for name in os.listdir(path)]
newest = max(filenames, key = os.path.getmtime)
Note that if you do it this way, then newest will be a full path name, so you would then not need os.path.join in the shelve.open line:
data = shelve.open(newest, flag = "w")
By the way, an alternative to using full path names is to change the current directory:
os.chdir(path)
Although this looks simpler, it can also make your code harder to comprehend, since the reader has to keep track of what the current working directory is.
Perhaps this is not hard if you only call os.chdir once, but in a complicated script, calling os.chdir in many places can make the code a bit spaghetti-like.
By using full path names there is no question about what you are doing.
If you wish to open each file:
import os
import contextlib
filenames = [os.path.join(path,name) for name in os.listdir(path)]
for filename in filenames:
with contextlib.closing(shelve.open(filename, flag = "w")) as data:
# do stuff with data
# ...
# data.close() will be called for you when Python leaves this with-block
max(). Is the DB location fixed (as in, it won't change during the course of running the program)?