Setting Current Dir in Python

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Sep 4 15:26:53 EDT 2007


En Tue, 04 Sep 2007 09:12:22 -0300, Sandipan Gangopadhyay  
<sandipan at earthlink.net> escribi�:

> I have recently installed Python 2.5.1 and Pythonwin (without any  
> errors) on
> Windows Vista Ultimate.
>
> Now, the programs run fine within Pythonwin IDE when current directory is
> set to the program's directory.
>
> But, when I double click them from Windows Explorer, the program is not  
> run.

First, fix your source code (either removing the non-ascii character or  
adding an encoding line at the top, see  
<http://www.python.org/peps/pep-0263.html>) (PEP0263 is enforced from  
Python 2.5 and up; you got a warning on earlier versions).

os.getcwd() returns the current directory.
os.chdir() changes the current directory.
A shortcut on the desktop may use the desktop itself as the initial  
directory.
If you want to run your script from within the same directory as it  
resides, try this:
os.chdir(os.path.dirname(__file__))


-- 
Gabriel Genellina




More information about the Python-list mailing list