[Tutor] OSError

Christopher Spears cspears2002 at yahoo.com
Wed Mar 15 06:21:07 CET 2006


I am trying to write a function that takes a directory
name and describes the contents of the directory (file
name and size) recursively.  Here is what I have
written so far:

import os, os.path

def describeDirectory(directory):
    if os.listdir(directory) == []:
        print "Empty directory!"
    else:
        for files in os.listdir(directory):
            if os.path.isdir(files):
                print files, ' is a directory!'
            else:
                print files, 'SIZE: ',
os.path.getsize(files)

print 'Current Directory: \n'
describeDirectory('.')
print '\n'
print './testFiles: \n'
describeDirectory('./testFiles')

Here is the result:

Current Directory: 

changePeppers.py SIZE:  915
describeDirectory.py SIZE:  481
describeDirectory.pyc SIZE:  514
error_log SIZE:  0
makezeros.py SIZE:  147
makezeros.pyc SIZE:  481
output SIZE:  387
pepper.txt SIZE:  601
testFiles  is a directory!
textWrapper.py SIZE:  619
textWrapper.pyc SIZE:  1092
timings.py SIZE:  567
timings.pyc SIZE:  733


./testFiles: 

renameFiles.py SIZE: 
Traceback (most recent call last):
  File "describeDirectory.py", line 17, in ?
    describeDirectory('./testFiles')
  File "describeDirectory.py", line 11, in
describeDirectory
    print files, 'SIZE: ', os.path.getsize(files)
  File "/usr/lib/python2.4/posixpath.py", line 139, in
getsize
    return os.stat(filename).st_size
OSError: [Errno 2] No such file or directory:
'renameFiles.py'

I don't understand the OSError.  The file
'renameFiles.py' is in './testFiles' directory.  Can
someone clarify this for me?

-Chris

"I'm the last person to pretend that I'm a radio.  I'd rather go out and be a color television set."
-David Bowie

"Who dares wins"
-British military motto

"I generally know what I'm doing."
-Buster Keaton


More information about the Tutor mailing list