查找用户的“我的文档"小路 [英] Finding the user's "My Documents" path
本文介绍了查找用户的“我的文档"小路的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个小程序,它需要在他们的我的文档"文件夹中创建一个小的 .txt 文件.这是我的代码:
I have this small program and it needs to create a small .txt file in their 'My Documents' Folder. Here's the code I have for that:
textfile=open('C:\Users\MYNAME\Documents','w')
lines=['stuff goes here']
textfile.writelines(lines)
textfile.close()
问题是,如果其他人使用它,我如何将MYNAME
更改为他们的帐户名?
The problem is that if other people use it, how do I change the MYNAME
to their account name?
推荐答案
使用os.path.expanduser(path)
,见http://docs.python.org/library/os.path.html
例如expanduser('~/filename')
根据文档,这适用于 Unix 和 Windows.
This works on both Unix and Windows, according to the docs.
由于斯文的评论,正斜杠.
forward slash due to Sven's comment.
这篇关于查找用户的“我的文档"小路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文