[Pythonmac-SIG] cwd and PythonLauncher problem--behavior variability is Bad 

Jim Harrison jhrsn at pitt.edu
Fri Oct 1 23:27:17 CEST 2004


on 10/1/04 2:03 PM, Bob Ippolito at bob at redivi.com wrote:

>>> In the meantime, you could do:
>>> import os
>>> os.chdir(os.path.dirname(__file__))
>>> which will change to the directory that your script is in.
>>
>> Chris Barker wrote:
>>
>> Is there a reliable way to change it to the user's home directory? can
>> you count on this?:
>> 
>> import os
>> os.chdir(os.getenv("HOME"))

Thanks very much for the responses.

While Bob and Chris's workarounds are effective, I think this is an issue
that could use a bit of attention because of existing inconsistencies:

In the statement: myFileHandle = open('spam.txt', 'r')

1. When a script is run from IDLE or MacPythonIDE (OSX 10.3.5), open finds
or creates spam.txt in the directory containing the script file.

2. When a script is run in Windows either from IDLE or by double-clicking,
spam.txt is also found or created in the directory containing the script.

3. When the script is double-clicked on OSX, drag/dropped on PythonLauncher,
or run from PyOXIDE or BBEdit, open does not find spam.txt in the directory
of the script or the file is created at / (once I had a file created in my
home directory and I haven't tried to track that behavior down further).

4. Some of the existing Python documentation, such as the second edition of
O'Reilly's Learning Python, states explicitly that the default file path for
file names in the situation above is the directory the file is executing
from.

5. I think most users would feel intuitively that navigating the command
line to a directory (using cd) and then calling python to run a file in that
directory should behave similarly to navigating to a directory through the
GUI and double clicking a python script in that directory (assuming file
types are set so that the script is passed to the interpreter). This
currently is the case on Windows but not on OSX. My hazy memory is that the
classic PythonLauncher did set the default to the directory containing the
file.

Without getting into an argument about who is doing it the right way, this
variability in documentation and behavior can be confusing to new Python
users (I'm currently teaching an introductory Python class) and overall is
probably Not A Good Thing.

Jim Harrison
Univ. of Pittsburgh


More information about the Pythonmac-SIG mailing list