如何设置当前用户的文件路径? [英] How do I set the file path to the current user?

查看:109
本文介绍了如何设置当前用户的文件路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在获取目录路径,

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 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆