os.path weirdness

Peter Hansen peter at engcorp.com
Tue Jun 17 18:29:35 CEST 2003

Jeff Layton wrote:
> #!/usr/bin/python
> import os.path
> filep="/home1/laytojb/TEMP/tet.out"
> print "type: ",type(filep)
> if os.path.isfile(filep): #{
>      print "Found tet.out"
> else:
>      print "Cannot find tet.out"
> #}
> This simple script works correctly and finds the
> file tet.out.
>     However, when I do virtually the same thing
> in a much longer script, it can never find the
> file, even though it exists. 

Since, clearly, these functions work in most cases, you'd
better post the relevant tiny section of the larger
script along with the name of the file it is looking for
and see what folks say.

I'd suggest the following:  change that large script, 
to include printing the results of "os.path.exists()"
as well, plus "os.path.isdir()" for good measure, and
also a call to "os.system('ls -l %s' % filep)" where
of course you use the same filep variable that the rest
of the script is operating on.

> I've checked my spelling
> many times, I've checked that the argument to
> os.path.isfile() is a string with no blank spaces
> (used string.stip() to peel off leading and trailing
> spaces). I still can't seem to find the file.

Another possibility is to learn to use the Python debugger
enough to troubleshoot this kind of thing.  Try this:

1. place "import pdb; pdb.set_trace()" just above the
line which is failing.

2. Run your code from the shell.

3. When it gets to the new lines, it will dump you into the
pdb prompt.  At this point carefully hit "r" and ENTER to
get out of the set_trace() routine's context, and up to the
level of the calling code.  You are now sitting in an 
interactive environment that lets you examine variables
and execute statements, or single step through the code.

4. Open a new shell and double-check that the file is really
there at this point in time.

5. Check the value in filep.  Also explore pdb a bit while
you're there.  Typing dir() is always a good help.

6. Step over the line containing the failing test using "n".  
See where it goes.  If it really can't find the file, you can
execute exactly the same statement (use the ! command as 
in "!os.path.isfile(filep)" or something like that) and
double-check what is going on.

7. Worst case, go back and do it again, only this time hit
"s" instead of "n" to single-step into the subroutine and
see what the heck it's doing.

By the way, you're not getting caught by something simple like
case-sensitivity under Linux, are you?


More information about the Python-list mailing list