473,224 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 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 5712
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Noah | last post by:
I need to create Stuffit (.sit) files on Linux. Does anyone have any ideas for how to do this? I checked the Python docs and on SourceForge, but I didn't see any open source stuffit compatible...
1
by: Tom | last post by:
I created an msi using the Setup Wizard, but don't seem to be able to create a shortcut to the applicaton on the desktop (or prompt for one) or the menu. Also, I would like to add an "uninstall"...
3
by: EC | last post by:
I am trying to save a file to an end users desktop from the Server on the click of a button. This works when run the application on my local but does not work when the application is running on the...
4
by: I_AM_DON_AND_YOU? | last post by:
There is one more problem I am facing but didn't get the solution. In my Setup Program I am not been able to create 2 things (when the program is intalled on the client machine ) : (1) create...
3
by: Michael D. Murphy | last post by:
Hi, I am searching for a way to creat desktop shortcuts when running the setup files that were created within a VB solution using the setup wizard. Thanks, Michael
7
by: GrandpaB | last post by:
I would appreciate assistance learning how to create a Desktop shortcut in my setup project. In the left pane of the Setup/File System window I right-clicked User's Desktop. From the contex...
8
by: paraidy | last post by:
Hi all, as from object i need to read all byte from a file example c: \myphoto.jpg and recreate the file with another name to another directory c:\photo\recreatedphoto.jpg can someone write a...
1
by: pbrown | last post by:
Hi, I'm using VB.net 2003, and I have a problem. I need a way of saving a file directly to the desktop, without hardcoding the direct path to the desktop into the program. For example, I'm trying...
3
by: sanghavi | last post by:
how to create a set up project in vb.net..how to run an application on a different machine
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.