[Tutor] TypeError: 'str' object is not callable

Tony Cappellini tony at tcapp.com
Sun Sep 7 00:44:21 EDT 2003



Hello,

I wrote the module below, to walk through a directory tree, and display 
certain FILE attributes that I'm looking for (this part isn't done yet)
While executing this, I would press Ctrl-C to abort execution, when I found 
what I was looking for, but didn't like the Ctrl-C exception.
So I added the the try-except, and a call to exit(), so the program would 
exit when i press Ctrl-C.

This error message is displayed when either of the calls to exit() are 
executed- but I don't understand why.

Exiting
Traceback (most recent call last):
   File "walk.py", line 19, in ?
   File "E:\apps\Python\lib\ntpath.py", line 324, in walk
     walk(name, func, arg)
   File "E:\apps\Python\lib\ntpath.py", line 318, in walk
     func(arg, top, names)
   File "walk.py", line 15, in printdirname
TypeError: 'str' object is not callable


Would someone please explain which str object is being called ?

thanks





import os
import time

def    printdirname(junk, dirpath, namelist):

        try:
            for name in namelist:
                if( os.path.isdir(name) ):
                         print "%s" % name

        except KeyboardInterrupt:
            print "\nExiting"
            exit(0)

if __name__ == '__main__':
    try:
        os.path.walk(r"e:\src\python", printdirname, "*.*")
    except KeyboardInterrupt:
        print"\nExiting from Exception during call to os.path.walk()"
        exit(0)




More information about the Tutor mailing list