file open default location

Tim Golden mail at timgolden.me.uk
Tue Jun 12 10:31:17 EDT 2007


T. Crane wrote:
> myFile = file('test.txt','w')
> 
> Here I'm opening/creating a file but I have not specified the exact path, so 
> how does Python determine where to 'put' this file?  More to the point, how 
> do I change what the default path is?  Right now it's a networked drive that 
> should not be getting my Python clutter.

Python doesn't choose anything. Whatever your
Operating System deems the current drive when
you start Python is the one which will contain
any other unqualified files. You can find out
what it is by running a script which just does:

import os
print os.getcwd ()

and you can change it by doing this:

import os
os.chdir ("new-path-of-my-choosing")

> Interestingly, this network drive is also where I can find my _ipython 
> folder from my ipython install as well as my .matplotlib folder.  Can anyone 
> tell me how to change where these folders and files go by default?

Different question. (And, I'm afraid, a more complicated one). You
haven't said, but I'm going to guess you're running on Windows,
not least because any *nix setup I know of will place the user in
a well-known "Home" directory (typically /home/username).

The trouble is that applications like ipython, and maybe
matplotlib, were developed under *nix where you can rely
on getting hold of a user's "Home" directory either by
expanding the "~" shell variable -- or whatever it's called --
or by examining the HOME shell variable. Windows doesn't
traditionally have either of these things, and has over
the years had several locations with legitimate claim to
be "Home".

Python's own os.expanduser, for example, uses this approach:
"""
On Windows, only "~" is supported; it is replaced by the
environment variable HOME or by a combination of
HOMEDRIVE and HOMEPATH
"""

I think IPython now uses expandvar. Maybe it always did;
I've an idea its current behaviour was a more recent
addition to Python under Windows. But IPython used to
fall back to C:\ if it couldn't do anything else.
Don't know about matplotlib. You'll need to check the
docs (or the source).

TJG



More information about the Python-list mailing list