473,387 Members | 1,834 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,387 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 5742
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
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.