470,815 Members | 1,340 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

Oserror: [Errno 20]

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:
Expand|Select|Wrap|Line Numbers
  1.  
  2. for subdir in os.listdir(DATADIR):              #loop through list of
  3. strings
  4.  
  5. file=FITS.Read(DATADIR+'/'+subdir+'/flux.fits')     #opens
  6. flux.fits file and reads
  7.  
  8. summation=open(DATADIR+'/'+subdir+'/flux.dat','w')  #opens the
  9. summation results file for writing to
  10.  
  11. spotbyspot=open(DATADIR+'/'+subdir+'/spotflux.dat','w') #opens the
  12. spot-by-spot file for writing to
  13.  
  14. output=''
  15. print'\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n'+sys.argv[1]+'
  16. '+subdir+'\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n'
  17.  
  18.  
The first line is the one with the error. Can anyone help?

Thanks!
Apr 3 '06 #1
10 5589
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"
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


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

Apr 3 '06 #2
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 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"

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

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

Apr 3 '06 #3
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
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Apr 3 '06 #4
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 Stuvel wrote:
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

Apr 3 '06 #5
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
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Apr 3 '06 #6
I think that if you go back and look at the original reply, he spelled
it "isdir"...;)

--Ben
k r fry wrote:
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 Stuvel wrote:
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

Apr 3 '06 #7
Thank you very much! *embarassed*.

:-)

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

--Ben
k r fry wrote:
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 Stuvel wrote:
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

Apr 3 '06 #8
k r fry wrote:
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

Apr 3 '06 #9
Thank you very much! I really appreciate the help. :)
Apr 3 '06 #10
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
sc***********@acm.org
Apr 3 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Mark Harrison | last post: by
3 posts views Thread by Scott Whitney | last post: by
reply views Thread by Joram Agten | last post: by
4 posts views Thread by David Hirschfield | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.