如何设置当前用户的文件路径? [英] How do I set the file path to the current user?
问题描述
现在获取目录路径,
os.chdir(r'C:\users\Ryan\AppData\Local\Google\Chrome\Application')
我该如何使其代替 Ryan,而是使用使用脚本的人的用户名?
How do I make it so that instead of "Ryan" it uses the username of the person using the script?
推荐答案
看看< os.path $的code> expanduser
c $ c> :
Take a look at expanduser
of os.path
:
os.path.expanduser(path)
在Unix和Windows上,返回带有〜或〜user初始组成部分的参数,并替换为该用户的主目录。
On Unix and Windows, return the argument with an initial component of ~ or ~user replaced by that user‘s home directory.
[..]
在Windows上,将使用HOME和USERPROFILE(如果已设置),否则将使用HOMEPATH和HOMEDRIVE的组合。最初的〜user
可以通过从上面派生的
用户路径中剥离最后一个目录组件来处理。
On Windows, HOME and USERPROFILE will be used if set, otherwise a combination of HOMEPATH and HOMEDRIVE will be used. An initial ~user is handled by stripping the last directory component from the created user path derived above.
如果扩展失败或路径不是以波浪号开头,路径将保持不变。
If the expansion fails or if the path does not begin with a tilde, the path is returned unchanged.
这篇关于如何设置当前用户的文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!