Oserror: [Errno 20]

K

k r fry

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

     summation=open(DATADIR+'/'+subdir+'/flux.dat','w')  #opens the 
summation results file for writing to

     spotbyspot=open(DATADIR+'/'+subdir+'/spotflux.dat','w') #opens the 
spot-by-spot file for writing to

     output=''
     print'\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n'+sys.argv[1]+' 
'+subdir+'\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n'

The first line is the one with the error. Can anyone help?

Thanks!
 
P

Peter Hansen

k said:
Hi, I am new to this list and also programming with python.

I have an error: oserror [errno 20] not a directory "katiescint.py"

Please always post complete tracebacks, and without manually reentering
the data. In most cases you'll save us and you lots of time and guessing.
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[/QUOTE]

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.

-Peter
 
K

k r fry

Sorry for not copying the whole traceback before, I hop I have done it
right this time.

I am now getting:

Traceback (most recent call last):
File "katiescint.py", line 153, in ?
for subdir in os.path.istdir(DATADIR): #loop through
list of strings
AttributeError: 'module' object has no attribute 'istdir'

I did think maybe it was meant to be "listdir" instead of "istdir", but
that doesn't work either.
Sorry to be a pain.


Peter said:
k said:
Hi, I am new to this list and also programming with python.

I have an error: oserror [errno 20] not a directory "katiescint.py"


Please always post complete tracebacks, and without manually reentering
the data. In most cases you'll save us and you lots of time and guessing.
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[/QUOTE]


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.

-Peter
[/QUOTE]
 
S

Sybren Stuvel

k r fry enlightened us with:
I did think maybe it was meant to be "listdir" instead of "istdir",
but that doesn't work either.

And again you don't tell us in what way it doesn't work.

Think about what you post from our point of view. Then re-read it, and
think about it again. Only if you're sure that we'll be able to fully
understand it, hit the 'send' button. That will save us a lot of
guessting and asking, and will also get you your answer a lot faster.

Sybren
 
K

k r fry

Again, I apologise. Not knowing much about Python means that I don't
know what needs to be provided. I meant it doesn't work in the same way
that "istdir" didn't work.

Here is what I have coded:

for subdir in os.path.istdir(DATADIR): #loop through list
of strings

file=FITS.Read(DATADIR+'/'+subdir+'/flux.fits') #opens
flux.fits file and reads

summation=open(DATADIR+'/'+subdir+'/flux.dat','w') #opens the
summation results file for writing to

spotbyspot=open(DATADIR+'/'+subdir+'/spotflux.dat','w') #opens the
spot-by-spot file for writing to

output=''
print'\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n'+sys.argv[1]+'
'+subdir+'\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n'

And here is what I get when I try to run it:

Traceback (most recent call last):
File "katiescint.py", line 153, in ?
for subdir in os.path.istdir(DATADIR): #loop through
list of strings
AttributeError: 'module' object has no attribute 'istdir'

Here is trying with listdir:

for subdir in os.path.listdir(DATADIR): #loop through list
of strings

and here is what I get:

Traceback (most recent call last):
File "katiescint.py", line 153, in ?
for subdir in os.path.listdir(DATADIR): #loop through
list of strings
AttributeError: 'module' object has no attribute 'listdir'
 
S

Sybren Stuvel

k r fry enlightened us with:
Traceback (most recent call last):
File "katiescint.py", line 153, in ?
for subdir in os.path.listdir(DATADIR): #loop through
list of strings
AttributeError: 'module' object has no attribute 'listdir'

But why do you use that function then? listdir is in the module os,
not in os.path.

Sybren
 
B

Ben Thul

I think that if you go back and look at the original reply, he spelled
it "isdir"...;)

--Ben
k said:
Again, I apologise. Not knowing much about Python means that I don't
know what needs to be provided. I meant it doesn't work in the same way
that "istdir" didn't work.

Here is what I have coded:

for subdir in os.path.istdir(DATADIR): #loop through list
of strings

file=FITS.Read(DATADIR+'/'+subdir+'/flux.fits') #opens flux.fits
file and reads

summation=open(DATADIR+'/'+subdir+'/flux.dat','w') #opens the
summation results file for writing to

spotbyspot=open(DATADIR+'/'+subdir+'/spotflux.dat','w') #opens the
spot-by-spot file for writing to

output=''
print'\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n'+sys.argv[1]+'
'+subdir+'\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n'

And here is what I get when I try to run it:

Traceback (most recent call last):
File "katiescint.py", line 153, in ?
for subdir in os.path.istdir(DATADIR): #loop through
list of strings
AttributeError: 'module' object has no attribute 'istdir'

Here is trying with listdir:

for subdir in os.path.listdir(DATADIR): #loop through list
of strings

and here is what I get:

Traceback (most recent call last):
File "katiescint.py", line 153, in ?
for subdir in os.path.listdir(DATADIR): #loop through
list of strings
AttributeError: 'module' object has no attribute 'listdir'




Sybren said:
k r fry enlightened us with:




And again you don't tell us in what way it doesn't work.

Think about what you post from our point of view. Then re-read it, and
think about it again. Only if you're sure that we'll be able to fully
understand it, hit the 'send' button. That will save us a lot of
guessting and asking, and will also get you your answer a lot faster.

Sybren
 
K

k r fry

Thank you very much! *embarassed*.

:)

Ben said:
I think that if you go back and look at the original reply, he spelled
it "isdir"...;)

--Ben
k said:
Again, I apologise. Not knowing much about Python means that I don't
know what needs to be provided. I meant it doesn't work in the same
way that "istdir" didn't work.

Here is what I have coded:

for subdir in os.path.istdir(DATADIR): #loop through list
of strings

file=FITS.Read(DATADIR+'/'+subdir+'/flux.fits') #opens
flux.fits file and reads

summation=open(DATADIR+'/'+subdir+'/flux.dat','w') #opens the
summation results file for writing to

spotbyspot=open(DATADIR+'/'+subdir+'/spotflux.dat','w') #opens the
spot-by-spot file for writing to

output=''
print'\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n'+sys.argv[1]+'
'+subdir+'\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n'

And here is what I get when I try to run it:

Traceback (most recent call last):
File "katiescint.py", line 153, in ?
for subdir in os.path.istdir(DATADIR): #loop through
list of strings
AttributeError: 'module' object has no attribute 'istdir'

Here is trying with listdir:

for subdir in os.path.listdir(DATADIR): #loop through
list of strings

and here is what I get:

Traceback (most recent call last):
File "katiescint.py", line 153, in ?
for subdir in os.path.listdir(DATADIR): #loop through
list of strings
AttributeError: 'module' object has no attribute 'listdir'




Sybren said:
k r fry enlightened us with:

I did think maybe it was meant to be "listdir" instead of "istdir",
but that doesn't work either.




And again you don't tell us in what way it doesn't work.

Think about what you post from our point of view. Then re-read it, and
think about it again. Only if you're sure that we'll be able to fully
understand it, hit the 'send' button. That will save us a lot of
guessting and asking, and will also get you your answer a lot faster.

Sybren
 
P

Peter Hansen

k said:
Sorry for not copying the whole traceback before, I hop I have done it
right this time.

I am now getting:

Traceback (most recent call last):
File "katiescint.py", line 153, in ?
for subdir in os.path.istdir(DATADIR): #loop through
list of strings
AttributeError: 'module' object has no attribute 'istdir'

I did think maybe it was meant to be "listdir" instead of "istdir", but
that doesn't work either.
Sorry to be a pain.

You'll get the hang of it... :)

There are two functions required here: os.listdir(), and
os.path.isdir(). The first one is the correct one for the purpose, as
you originally used it. The second one (which I suppose someone with a
German background might try to spell "istdir" ;-) ) is to test whether a
given path "is [a] dir[ectory]".

If you read the documentation on those two functions, and experiment at
the interactive prompt, you'll shortly notice that os.listdir() returns
only the names of the items in the directory, not the full paths.
os.path.isdir(), on the other hand, requires a full path (since of
course it can't know what directory you looked in with os.listdir), so
you'll have to join the names together before testing them, something
like this (and I'll rely on you to actually *read the docs* and
*experiment at the interactive prompt* this time, instead of giving up
so quickly and asking for help again when you could figure this out
yourself with a little more effort):

for name in os.listdir(DATADIR):
path = os.path.join(DATADIR, name)
if os.path.isdir(path):
filename = os.path.join(path, 'flux.fits')
data = FITS.Read(filename)
# etc....

-Peter
 
S

Scott David Daniels

Peter said:
k said:
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[/QUOTE]

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.[/QUOTE]

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
[email protected]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top