【发布时间】:2012-04-16 07:51:30
【问题描述】:
可能重复:
How to find the real user home directory using python?
How to get the home directory in Python?
我想访问/home/weasel 以从那里读取一些文件,但我当然不想写完整路径 - 所以其他用户可以使用脚本.. 你怎么知道你的用户名或你的主目录Linux上的python?
谢谢
【问题讨论】:
-
如果有人只想要主目录而不是用户目录,您可以尝试查找用户目录 (
ud) 并执行以下操作:hd=os.sep.join(ud.split(os.sep)[:-1])