[Tutor] OSError

Ewald Ertl ewald.ertl at hartter.com
Wed Mar 15 09:03:26 CET 2006


Hi Chris,
	  I think I've found the problem. os.listdir() gives you all entries of the directory, but your
not changing to that directory. The os.path.getsize() just does not find the file in the directory "testFiles" under
your current directory.
See my short example below.


>>> import os
>>> os.listdir( "trace" )
['nbewer', 'installer_acb_2004-10-15.zip', 'epAnalyze.py', 'nbgene', 'ep20051201.txt', 'IPseconLinux.sxw', 'bbaseline', 'inner-classes.patch', 'jde-usages-0.9.1.zip', 'bleuch.ttf', 'privdebug']
>>> os.getcwd()
'/users/trinomic/ewer'
>>> import os.path
>>> os.path.isfile ( "epAnalyze.py" )
False
>>> os.path.isfile ( "trace/epAnalyze.py" )
True


HTH Ewald

Christopher Spears wrote:
> 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
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 




More information about the Tutor mailing list