How to create a file on users XP desktop

Matimus mccredie at gmail.com
Sun Oct 7 01:32:49 EDT 2007


On Oct 6, 8:31 pm, goldtech <goldt... at worldpost.com> wrote:
> Can anyone link me or explain the following:
>
> I open a file in a python script. I want the new file's location to be
> on the user's desktop in a Windows XP environment.  fileHandle = open
> (....., 'w' )  what I guess I'm looking for is an environmental
> variable that will usually be correct on most XP desktops and will
> work in the open statement. Or is there another way?
>
> Thanks

I've always used 'USERPROFILE'. I'm not sure how standard it is, but I
have never run into any issues.

Code for your perusal:
[code]
import os, os.path
prof_path = os.environ['USERPROFILE']
filename = os.path.join(prof_path,'Desktop','filename.txt')
f = open(filename,'w')
try:
    # do stuff with f
finally:
    f.close()
[/code]

Matt




More information about the Python-list mailing list