faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 2 people (50%) answered Yes
Recently 0 of 1 people (0%) answered Yes

Entry

Pathnames on Windows

Jul 5th, 2000 10:01
Nathan Wallace, Hans Nowak, Snippet 178, Bill Tutt


"""
Packages: operating_systems.windows
"""
"""
> > > > OK.  Will giving the filename ".\\intriguer" work for 
> this?  And,
> > > > given that the filename is going through the C library, 
> > will leaving
> > > > it as "./intriguer" work?
> > > 
> 
> Well, as long as your current working directory is what you 
> want it to be, sure it'll work, otherwise you'll need to pull 
> the standard "where did I get run from" nonsense.
> 
Something like the following usually does the trick:
"""
import os
import sys
(head, tail) = os.path.split(sys.argv[0])
# If tail is None, then we just got invoked in the current working directory.
if tail is None:
	path = os.getcwd()
else:
# Otherwise, head now contains a possibly relative path to where we were 
# invoked from, normalize it.
	path = os.path.abspath(head)
inifile = open('%s/blah.ini' % path, 'r')