如何在用户XP桌面上创建文件 [英] How to create a file on users XP desktop
问题描述
任何人都可以链接我或解释以下内容:
我在python脚本中打开一个文件。我希望Windows XP环境中用户桌面上新文件的位置为
。 fileHandle = open
(.....,''w'')我想我正在寻找的是一个环境
变量通常是正确的在大多数XP桌面上,并且
在open语句中工作。还是有另一种方式吗?
谢谢
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
推荐答案
10月6日,11:晚上31点,goldtech< goldt ... @ worldpost.comwrote:
On Oct 6, 11:31 pm, goldtech <goldt...@worldpost.comwrote:
任何人都可以链接我或解释以下内容:
我在python脚本中打开一个文件。我希望Windows XP环境中用户桌面上新文件的位置为
。 fileHandle = open
(.....,''w'')我想我正在寻找的是一个环境
变量通常是正确的在大多数XP桌面上,并且
在open语句中工作。或者还有另一种方式吗?
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?
这实际上是一个Windows问题,而不是Python问题。您应该通过检查现有的
环境变量来自行解决这个问题。
下面的代码应该对你有用标准
环境变量没有被软管。
----------
import os >
Filename = os.getenv(" HOMEDRIVE")+ os.getenv(" HOMEPATH")+" \\Desktop
\ MyNewFile"
f = file(文件名," w")
f.write(这是桌面上的文件\ n)
f .close()
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()
goldtech写道:
goldtech wrote:
...我想要新文件
a Windows XP环境中位于用户桌面上的位置....
... I want the new file''s location to be on the user''s desktop in
a Windows XP environment....
怎么样 :
import os.path
handle = open(os.path.expanduser(r''〜\\\\\\\\\\\\\\\\\\\\\\\\\\\ ,''w'')
...
-Scott
How about:
import os.path
handle = open(os.path.expanduser(r''~\DeskTop\somefile.txt'') , ''w'')
...
-Scott
10月6日晚上8:31,goldtech< goldt .. 。@ worldpost.comwrote:
On Oct 6, 8:31 pm, goldtech <goldt...@worldpost.comwrote:
任何人都可以链接我或解释以下内容:
我在python中打开一个文件脚本。我希望Windows XP环境中用户桌面上新文件的位置为
。 fileHandle = open
(.....,''w'')我想我正在寻找的是一个环境
变量通常是正确的在大多数XP桌面上,并且
在open语句中工作。或者还有另一种方式吗?
谢谢
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
我一直使用''USERPROFILE''。我不确定它有多标准,但是我没有遇到任何问题。
您的细读代码:
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:
这篇关于如何在用户XP桌面上创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!