| How to create a file on users XP desktop [message #15591] |
Sat, 06 October 2007 23:31  |
goldtech Messages: 15 Registered: August 2007 |
Junior Member |
|
|
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
|
|
|
|
|
|
|
|
| Re: How to create a file on users XP desktop [message #15651 is a reply to message #15591 ] |
Sun, 07 October 2007 08:03   |
ricaraoz Messages: 60 Registered: July 2007 |
Member |
|
|
Matimus wrote:
> On Oct 6, 8:31 pm, goldtech <goldt...@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:
>
> 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()
>
>
> Matt
>
You are assuming the system is not localized, that won't work if you
distribute your applications internationally. In my system it is not
"Desktop", it is "Escritorio", and I guess it will vary with every
locale. Does someone know a way to find out what name does the desktop
have?
|
|
|
|
|
| Re: How to create a file on users XP desktop [message #15772 is a reply to message #15604 ] |
Sun, 07 October 2007 13:30  |
Tim Golden Messages: 51 Registered: August 2007 |
Member |
|
|
Tim Chase wrote:
>> You are assuming the system is not localized, that won't work if you
>> distribute your applications internationally. In my system it is not
>> "Desktop", it is "Escritorio", and I guess it will vary with every
>> locale. Does someone know a way to find out what name does the desktop
>> have?
>
> I believe you need to read the Desktop value from
>
> """
> HKEY_CURRENT_USER\software\microsoft\windows\currentversion\ explorer\shell
> folders
> """
>
> which should (in my understanding) hold the full path of the
> particular folder(s) of interest at that location, including
> across localizations.
>
> -tkc
Ideally, use the shell API exposed by pywin32:
<code>
from win32com.shell import shell, shellcon
desktop = shell.SHGetFolderPath (0, shellcon.CSIDL_DESKTOP, 0, 0)
</code>
For slight convenience:
http://timgolden.me.uk/python/winshell.html
TJG
|
|
|
| Re: How to create a file on users XP desktop [message #15774 is a reply to message #15604 ] |
Sun, 07 October 2007 13:30  |
Tim Golden Messages: 51 Registered: August 2007 |
Member |
|
|
Tim Chase wrote:
>> You are assuming the system is not localized, that won't work if you
>> distribute your applications internationally. In my system it is not
>> "Desktop", it is "Escritorio", and I guess it will vary with every
>> locale. Does someone know a way to find out what name does the desktop
>> have?
>
> I believe you need to read the Desktop value from
>
> """
> HKEY_CURRENT_USER\software\microsoft\windows\currentversion\ explorer\shell
> folders
> """
>
> which should (in my understanding) hold the full path of the
> particular folder(s) of interest at that location, including
> across localizations.
>
> -tkc
Ideally, use the shell API exposed by pywin32:
<code>
from win32com.shell import shell, shellcon
desktop = shell.SHGetFolderPath (0, shellcon.CSIDL_DESKTOP, 0, 0)
</code>
For slight convenience:
http://timgolden.me.uk/python/winshell.html
TJG
|
|
|