【发布时间】:2011-02-09 18:24:04
【问题描述】:
我看到如果我们更改 HOME (linux) 或 USERPROFILE (windows) 环境变量并运行 python 脚本,它会在我尝试时返回新值作为用户主页
os.environ['HOME']
os.exp
有没有什么方法可以不依赖环境变量找到真正的用户主目录?
编辑:
这是一种通过读取注册表在windows中找到userhome的方法,
http://mail.python.org/pipermail/python-win32/2008-January/006677.html
编辑:
一种使用 pywin32 查找 windows home 的方法,
from win32com.shell import shell,shellcon
home = shell.SHGetFolderPath(0, shellcon.CSIDL_PROFILE, None, 0)
【问题讨论】:
-
您可能需要查看 unix 命令(快捷方式):
~user它会将您带到当前用户的主目录。在windows上不知道。 -
这应该被标记为 How to get the home directory in Python? 的副本,因为该问题的已接受答案适用于 Python 3,而该问题则不适用。
标签: python linux windows directory home-directory