Home » Coding » Python » How to create a file on users XP desktop
How to create a file on users XP desktop [message #15591] Sat, 06 October 2007 23:31 Go to next message
goldtech  is currently offline 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 #15599 is a reply to message #15591 ] Sun, 07 October 2007 00:34 Go to previous messageGo to next message
drobinow  is currently offline drobinow
Messages: 4
Registered: August 2007
Junior Member
On Oct 6, 11: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?
This is really a Windows question, not a Python question. You should
have been able to figure it out yourself by examining existing
environment variables.
Something like the code below should work for you if the standard
environment variables haven't been hosed.
----------
import os
Filename = os.getenv("HOMEDRIVE") + os.getenv("HOMEPATH") + "\\Desktop
\MyNewFile"
f = file(Filename, "w")
f.write("Here's a file on the desktop\n")
f.close()
Re: How to create a file on users XP desktop [message #15602 is a reply to message #15591 ] Sun, 07 October 2007 01:24 Go to previous messageGo to next message
Scott David Daniels  is currently offline Scott David Daniels
Messages: 20
Registered: August 2007
Junior Member
goldtech wrote:
> ... I want the new file's location to be on the user's desktop in
> a Windows XP environment....
>
How about:
import os.path
handle = open(os.path.expanduser(r'~\DeskTop\somefile.txt'), 'w')
...

-Scott
Re: How to create a file on users XP desktop [message #15604 is a reply to message #15591 ] Sun, 07 October 2007 01:32 Go to previous messageGo to next message
Matt McCredie  is currently offline Matt McCredie
Messages: 38
Registered: August 2007
Member
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
Re: How to create a file on users XP desktop [message #15624 is a reply to message #15591 ] Sun, 07 October 2007 05:12 Go to previous messageGo to next message
Craig Howard  is currently offline Craig Howard
Messages: 2
Registered: October 2007
Junior Member
On Oct 6, 2007, at 11:31 PM, goldtech 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
>
> --
> http://mail.python.org/mailman/listinfo/python-list

import os.path
home = os.path.expanduser('~')
desktop = os.path.join(home, 'Desktop')
Re: How to create a file on users XP desktop [message #15645 is a reply to message #15599 ] Sun, 07 October 2007 07:58 Go to previous messageGo to next message
goldtech  is currently offline goldtech
Messages: 15
Registered: August 2007
Junior Member
>
> This is really a Windows question, not a Python question. You should
> have been able to figure it out yourself by examining existing
> environment variables.

I agree, you're right. I learn more by figuring out the code myself.

After Google briefly: In a DOS box type: SET

This was too easy - sorry.



> Something like the code below should work for you if the standard
> environment variables haven't been hosed.
> ----------
> import os
> Filename = os.getenv("HOMEDRIVE") + os.getenv("HOMEPATH") + "\\Desktop
> \MyNewFile"
> f = file(Filename, "w")
> f.write("Here's a file on the desktop\n")
> f.close()
Re: How to create a file on users XP desktop [message #15651 is a reply to message #15591 ] Sun, 07 October 2007 08:03 Go to previous messageGo to next message
ricaraoz  is currently offline 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 #15746 is a reply to message #15602 ] Sun, 07 October 2007 12:21 Go to previous messageGo to next message
drobinow  is currently offline drobinow
Messages: 4
Registered: August 2007
Junior Member
On Oct 7, 1:24 am, Scott David Daniels <Scott.Dani...@Acm.Org> wrote:
> goldtech wrote:
> > ... I want the new file's location to be on the user's desktop in
> > a Windows XP environment....
>
> How about:
> import os.path
> handle = open(os.path.expanduser(r'~\DeskTop\somefile.txt'), 'w')
> ...

That doesn't work on my system.
"~" uses the HOME environment variable if set, otherwise uses
HOMEDRIVE and HOMEPATH which is what I posted before. See Lib/
ntpath.py in a recent source distribution.
In my case I set HOME to a path without spaces in it, to simplify my
use of Emacs and Cygwin.
Re: How to create a file on users XP desktop [message #15758 is a reply to message #15604 ] Sun, 07 October 2007 12:47 Go to previous messageGo to next message
Tim Chase  is currently offline Tim Chase
Messages: 21
Registered: September 2007
Junior Member
> 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
Re: How to create a file on users XP desktop [message #15772 is a reply to message #15604 ] Sun, 07 October 2007 13:30 Go to previous message
Tim Golden  is currently offline 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 Go to previous message
Tim Golden  is currently offline 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
Previous Topic:Pil image module, "mode" bug..
Next Topic:Putting a line from a text file into a variable, then moving to nextline
Goto Forum:
  


Current Time: Fri May 16 03:06:07 EDT 2008

Total time taken to generate the page: 0.20536 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 2.7.7.
Copyright ©2001-2007 FUD Forum Bulletin Board Software