Oserror: [Errno 20]
Scott David Daniels
scott.daniels at acm.org
Mon Apr 3 12:42:51 EDT 2006
Peter Hansen wrote:
> k r fry wrote:
>> Hi, I am new to this list and also programming with python.
>> I have an error: oserror [errno 20] not a directory "katiescint.py"
>> The piece of code causing the problem is:
>> [code]
>>
>> for subdir in os.listdir(DATADIR): #loop through list of
>> strings
>>
>> file=FITS.Read(DATADIR+'/'+subdir+'/flux.fits') #opens
>> flux.fits file and reads
>
> os.listdir() returns a list of all files and directories inside the
> directory specified (DATADIR), so you can't assume that "subdir" is
> always going to be a directory. Use os.path.isdir() to confirm that it
> is and ignore those items for which that function returns False.
I'd guess you might want to go with something like:
for root, dirs, files in os.walk(DATADIR):
if 'flux.fits' in files:
file = FITS.Read(os.path.join(root, 'flux.fits'))
read up on:
os.path.join
and
os.walk
The previous code will do lots of recursive walking in a deeply nested
directory structure. If you only want to look at single-level subdirs,
you could either do:
root, dirs, files = iter(os.walk(DATADIR)).next()
for subdir in dirs:
file = FITS.Read(os.path.join(root, subdir, 'flux.fits'))
or:
for root, dirs, files in os.walk(DATADIR):
if 'flux.fits' in files:
file = FITS.Read(os.path.join(root, 'flux.fits'))
if root != DATADIR:
del dirs[:] # Stop recursing after the first descent)
depending on whether you know 'flux.fits' should be there or
you only want to work on subdirs where it is in the directory.
-Scott David Daniels
scott.daniels at acm.org
More information about the Python-list
mailing list