OS independent files

Tim Chase python.list at tim.thechases.com
Thu Aug 3 15:51:20 EDT 2006


> location prior to pickling something to it.  But I have a question
> about it.  In Windows I can make a file with this:
> 
> os.path.join("C:", "myfiles", "myfile.dat")
> 
> If I want to make sure the file/directory is made in a user's home
> directory (e.g. /home/users/path/to/file) but also compatible w/
> Windows, how would I rewrite this (if required)?


Well, there in os.path you'll find expanduser() so you can do 
things like

 >>> homedir = os.path.expanduser("~")
 >>> filename = os.path.join(homedir, "myfiles", "myfile.dat")

Seems to work well for me.

-tkc






More information about the Python-list mailing list