newb question: file searching

John Machin sjmachin at lexicon.net
Tue Aug 8 18:15:05 EDT 2006


jaysherby at gmail.com wrote:

>  And, finally, the python docs all note that symbols like . and ..
> don't work with these commands.

Where did you read that? The docs for os.listdir do say that '.' and
'..' are (sensibly) not returned as *results*. AFAIK there is nothing
to stop you using '.' or '..' as a path *argument* where
appropriate/useful.

>  How can I grab the directory that my
> script is residing in?

C:\junk>type scriptdir.py
import sys, os.path
scriptpath = sys.argv[0] # read the docs for sys.argv
print "script is", scriptpath
scriptdir = os.path.split(scriptpath)[0] # read the docs for
os.path.split
print "script lives in", scriptdir
C:\junk>scriptdir.py
script is C:\junk\scriptdir.py
script lives in C:\junk

C:\junk>move scriptdir.py \bin

C:\junk>scriptdir.py
script is c:\bin\scriptdir.py
script lives in c:\bin

[Curiosity: drive letter is upper-case in first example, lower in
second]

HTH,
John




More information about the Python-list mailing list