Setting Current Dir in Python

Sandipan News news at sandipan.com
Tue Sep 4 21:11:50 EDT 2007


Tim and Gabriel,

Thank you so much. I was able to find and remove the special character from the line below and another one after it.
The error has now gone away.

Thanks also for the chdir solution, though I did not need it this time. The fix above was exactly what it needed to work.

I have my utilities chugging along again - What a relief!

Thanks again.

Sandipan

-----Original Message-----
From: python-list-bounces+news=sandipan.com at python.org [mailto:python-list-bounces+news=sandipan.com at python.org] On Behalf Of Gabriel Genellina
Sent: Tuesday, September 04, 2007 3:27 PM
To: python-list at python.org
Subject: Re: Setting Current Dir in Python

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

-- 
http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list