471,348 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

How to create a file on users XP desktop

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

Oct 7 '07 #1
11 5446
On Oct 6, 11:31 pm, goldtech <goldt...@worldpost.comwrote:
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()

Oct 7 '07 #2
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
Oct 7 '07 #3
On Oct 6, 8:31 pm, goldtech <goldt...@worldpost.comwrote:
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:
Expand|Select|Wrap|Line Numbers
  1. import os, os.path
  2. prof_path = os.environ['USERPROFILE']
  3. filename = os.path.join(prof_path,'Desktop','filename.txt')
  4. f = open(filename,'w')
  5. try:
  6. # do stuff with f
  7. finally:
  8. f.close()
  9.  
Matt

Oct 7 '07 #4

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')
Oct 7 '07 #5
>
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()

Oct 7 '07 #6
Matimus wrote:
On Oct 6, 8:31 pm, goldtech <goldt...@worldpost.comwrote:
>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:
Expand|Select|Wrap|Line Numbers
  1. import os, os.path
  2. prof_path = os.environ['USERPROFILE']
  3. filename = os.path.join(prof_path,'Desktop','filename.txt')
  4. f = open(filename,'w')
  5. try:
  6.     # do stuff with f
  7. finally:
  8.     f.close()
  9.  

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?
Oct 7 '07 #7
On Oct 7, 1:24 am, Scott David Daniels <Scott.Dani...@Acm.Orgwrote:
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.

Oct 7 '07 #8
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\curre ntversion\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


Oct 7 '07 #9
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\curre ntversion\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
Oct 7 '07 #10
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\curre ntversion\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
Oct 7 '07 #11
On Oct 7, 12:30 pm, Tim Golden <t...@timgolden.me.ukwrote:
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\curre ntversion\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
What happened to your winshell module? I would have thought it perfect
for this.

http://timgolden.me.uk/python/winshell.html

Mike

Oct 8 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Noah | last post: by
4 posts views Thread by I_AM_DON_AND_YOU? | last post: by
3 posts views Thread by Michael D. Murphy | last post: by
1 post views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.