Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to create a file on users XP desktop

1 view
Skip to first unread message

goldtech

unread,
Oct 6, 2007, 11:31:54 PM10/6/07
to
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

drob...@gmail.com

unread,
Oct 7, 2007, 12:34:40 AM10/7/07
to
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()

Scott David Daniels

unread,
Oct 7, 2007, 1:24:49 AM10/7/07
to
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

Matimus

unread,
Oct 7, 2007, 1:32:49 AM10/7/07
to

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:
[code]
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()
[/code]

Matt

Craig Howard

unread,
Oct 7, 2007, 5:12:58 AM10/7/07
to pytho...@python.org

> --
> http://mail.python.org/mailman/listinfo/python-list

import os.path
home = os.path.expanduser('~')
desktop = os.path.join(home, 'Desktop')

goldtech

unread,
Oct 7, 2007, 7:58:46 AM10/7/07
to

>
> 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.

Ricardo Aráoz

unread,
Oct 7, 2007, 8:03:00 AM10/7/07
to Matimus, pytho...@python.org

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?

drob...@gmail.com

unread,
Oct 7, 2007, 12:21:31 PM10/7/07
to

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.

Tim Chase

unread,
Oct 7, 2007, 12:47:02 PM10/7/07
to rar...@bigfoot.com, pytho...@python.org
> 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


Tim Golden

unread,
Oct 7, 2007, 1:30:57 PM10/7/07
to pytho...@python.org

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

Tim Golden

unread,
Oct 7, 2007, 1:30:36 PM10/7/07
to pytho...@python.org
Message has been deleted

kyos...@gmail.com

unread,
Oct 8, 2007, 10:02:39 AM10/8/07
to

What happened to your winshell module? I would have thought it perfect
for this.

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

Mike

goldtech

unread,
Oct 8, 2007, 10:19:43 AM10/8/07
to

> from win32com.shell import shell, shellcon
>
> desktop = shell.SHGetFolderPath (0, shellcon.CSIDL_DESKTOP, 0, 0)
>
> </code>

Tim,

How did you learn Win32com?

Other than the O'Reilly book, I've never found a lot of
documentation.

Trying to browse COM in PythonWin is tough - there's tons of stuff in
there. I've never been able to find the Win32com classes, methods,
usage examples when I browse COM in PythonWin.

For example where is, shell.SHGetFolderPath and shellcon.CSIDL_DESKTOP
officially documented?

Did you learn from using Visual C++ or VB? How did you learn this
stuff?

Thanks,
Lee G.

kyos...@gmail.com

unread,
Oct 8, 2007, 10:48:47 AM10/8/07
to

Pretty much the only place to learn stuff that's not in the PyWin32
docs is on one of the MSDN sites. Yes, that can suck. Here's the
general page: http://msdn2.microsoft.com/en-us/default.aspx

You can google for them too to get direct links to the MSDN page.

The ActiveState Python (AKA ActivePython) has an IDE that allows you
to browse the COM module. It also has a help file that allows you to
browse the PyWin32 docs locally. I think you can download that without
downloading ActivePython.

Mike

Tim Golden

unread,
Oct 8, 2007, 11:11:25 AM10/8/07
to pytho...@python.org

FWIW, the pywin32 distribution itself also comes with a local
.chm file. But aside from that, there have been several abortive
attempts -- including by Mike & myself! -- to get some kind of
online help going for pywin32, but nothing's really gained traction,
and we've all got more interesting things to be doing...

One point to bear in mind that, more or less, the pywin32 stuff
just wraps the MS API really closely, mostly doing just enough
of the messy plumbing to present the API "objects" as Python
objects. That's to say: find out how to do it from a C++ or VB
or Delphi tutorial and translating into Python often isn't hard.

As it happens I've been using Windows APIs for a few years,
so I have a bit of a head start. But I've answered quite
a few questions on python-win32 by putting the subject line
into Google, picking a likely-looking response and translating
it into Python.

In this case ("How to create a file on users XP desktop") the
question was too broad and tended to throw up user-oriented
answers. I tried a few permutations, including limiting the
search to msdn.microsoft.com, none of which showed much on the
first couple of pages. A search of the pywin32.chm files does
point in the right direction, but the fact is that the shell
functionality exposed by Windows which does this kind of
stuff is non-intuitive.

While I think everyone agrees that the Windows side of Python
could benefit from more and better docs, the general answer to:
How do I do X in Python under Windows? is: How do I X under Windows?

TJG

kyos...@gmail.com

unread,
Oct 8, 2007, 11:41:52 AM10/8/07
to
On Oct 8, 10:11 am, Tim Golden <m...@timgolden.me.uk> wrote:

I'm planning on working on that PyWin32 site some more this fall/
winter. Unfortunately, I shouldn't have volunteered my time during the
summer as I have very irregular a/c at home, and thus it was very
uncomfortable to use my PC when I was off work. Lame maybe, but I
dislike using a computer when it's above 90 degrees and the humidity
is higher than 70%. Bleh!

Sorry about that. I am experimenting with MediaWiki to see if it's any
faster than MoinMoin. If you know of a fast, easy-to-use wiki that's
better than both of those, let me know.

Mike

Julius

unread,
Oct 8, 2007, 1:16:19 PM10/8/07
to pytho...@python.org
On Monday 08 October 2007 17:11:25 Tim Golden wrote:
----------
Free version Zoner Photo Studio 9 - http://www.zps9.com/

Tim Golden

unread,
Oct 9, 2007, 4:24:53 AM10/9/07
to pytho...@python.org
>> On Oct 8, 9:19 am, goldtech <goldt...@worldpost.com> wrote:
>>> How did you learn Win32com?
>>>
>>> Other than the O'Reilly book, I've never found a lot of
>>> documentation.
>>>
>>> Trying to browse COM in PythonWin is tough - there's tons of stuff in
>>> there. I've never been able to find the Win32com classes, methods,
>>> usage examples when I browse COM in PythonWin.
>>>
>>> For example where is, shell.SHGetFolderPath and shellcon.CSIDL_DESKTOP
>>> officially documented?

Tim Golden wrote:
> FWIW, the pywin32 distribution itself also comes with a local
> .chm file. But aside from that, there have been several abortive
> attempts -- including by Mike & myself! -- to get some kind of
> online help going for pywin32, but nothing's really gained traction,
> and we've all got more interesting things to be doing...

To show willing :) I've added a howto on this subject to my
occasionally-updated list of Python win32 howto-s:

http://timgolden.me.uk/python/win32_how_do_i/get-the-desktop-folder.html

and a bonus, also from the shell area:

http://timgolden.me.uk/python/win32_how_do_i/browse-for-a-folder.html

I usually manage to do a few of these at one go once I gather
momentum, so hopefully I'll add a few more in today or tomorrow.

TJG

0 new messages