I am writing a command line dbm editor and have ran into a small issue. If I try to run certain commands (ie add, remove, display) when I do not have a file open, I get the following error:

Traceback (most recent call last):
  File "C:\Users\William\Desktop\dbmEditor.py.py", line 35, in <module>
    for key in file_edit.keys():
  File "C:\Python26\lib\bsddb\__init__.py", line 299, in keys
    self._checkOpen()
  File "C:\Python26\lib\bsddb\__init__.py", line 250, in _checkOpen
    raise error, "BSDDB object has already been closed"
DBError: BSDDB object has already been closed

This is fine and well but I need to do some error handling, but I do not know the syntax. Here is something like I would like it to look like:

if command[0] == 'display':
        try:
            for key in file_edit.keys():
                print repr(key), repr(file_edit[key])
        except DBError:
            print 'No file open'

but this does not work. Following is the whole code:

import anydbm
import tkFileDialog

while True:
    command = raw_input('> ')
    command = command.split(' ')
    if command[0] == 'open':
        file_edit = command[1]
        file_edit = anydbm.open(file_edit, 'c')
    if command[0] == 'opendialog':
       file_edit = tkFileDialog.askopenfilename()
       file_edit = anydbm.open(file_edit, 'c')
    if command[0] == 'close':
        file_edit.close
    if command[0] == 'display':
        try:
            for key in file_edit.keys():
                print repr(key), repr(file_edit[key])
        except DBError:
            print 'No file open'
    if command[0] == 'add':
        file_edit[command[1]] = command[2]
    if command[0] == 'remove':
        ask = raw_input('Remove entry? (y/n)')
        if ask == 'y':
            del file_edit[command[1]]
            print 'Entry Removed'
    if command[0] == 'close':
        file_edit.close()
    if command[0] == 'exit':
        exit()

from bsddb.db import DBError add that to your code.

Also you should change the last line to raise SystemExit or anything, unless if you actually have an exit() function.

So here's a better way to write the code:

import anydbm
import tkFileDialog
from bsddb.db import DBError

while True:
    command = raw_input('> ')
    command = command.split(' ')
    try:
        if command[0] == 'open':
            file_edit = command[1]
            file_edit = anydbm.open(file_edit, 'c')
        if command[0] == 'opendialog':
           file_edit = tkFileDialog.askopenfilename()
           file_edit = anydbm.open(file_edit, 'c')
        if command[0] == 'display':
            try:
                for key in file_edit.keys():
                    print key, file_edit[key]  #no need for repr()
            except DBError:
                print 'No file open'
        if command[0] == 'add':
            file_edit[command[1]] = command[2]
        if command[0] == 'remove':
            ask = raw_input('Remove entry? (y/n)')
            if ask == 'y':
                del file_edit[command[1]]
                print 'Entry Removed'
        if command[0] == 'close':
            file_edit.close()
        if command[0] == 'exit':
            raise SystemExit  #or sys.exit()
    except NameError:
        print 'Open a file first!'  #if the user types
                                    #display without
                                    #first opening a file
    except IndexError:
        print 'Invalid command.'    #if the user just
                                    #types "add", "remove",
                                    #or "open" by itself

from bsddb.db import DBError add that to your code.

Thanks, works perfectly. Also your other suggestion works as well

Also you should change the last line to raise SystemExit or anything, unless if you actually have an exit() function.

So here's a better way to write the code:

import anydbm
import tkFileDialog
from bsddb.db import DBError

while True:
    command = raw_input('> ')
    command = command.split(' ')
    try:
        if command[0] == 'open':
            file_edit = command[1]
            file_edit = anydbm.open(file_edit, 'c')
        if command[0] == 'opendialog':
           file_edit = tkFileDialog.askopenfilename()
           file_edit = anydbm.open(file_edit, 'c')
        if command[0] == 'display':
            try:
                for key in file_edit.keys():
                    print key, file_edit[key]  #no need for repr()
            except DBError:
                print 'No file open'
        if command[0] == 'add':
            file_edit[command[1]] = command[2]
        if command[0] == 'remove':
            ask = raw_input('Remove entry? (y/n)')
            if ask == 'y':
                del file_edit[command[1]]
                print 'Entry Removed'
        if command[0] == 'close':
            file_edit.close()
        if command[0] == 'exit':
            raise SystemExit  #or sys.exit()
    except NameError:
        print 'Open a file first!'  #if the user types
                                    #display without
                                    #first opening a file
    except IndexError:
        print 'Invalid command.'    #if the user just
                                    #types "add", "remove",
                                    #or "open" by itself

This works much better. Thanks for all the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.