Possible Duplicate:
How to find the real user home directory using python?
How to get the home directory in Python?
我想访问/home/weasel
从那里读取一些文件,但我当然不想写完整的路径,这样其他用户就可以使用脚本了..在Linux上使用python如何知道用户名或home dir?
谢谢
Possible Duplicate:
How to find the real user home directory using python?
How to get the home directory in Python?
我想访问/home/weasel
从那里读取一些文件,但我当然不想写完整的路径,这样其他用户就可以使用脚本了..在Linux上使用python如何知道用户名或home dir?
谢谢
要获得python中的homedir,可以使用os.path.expanduser('~')
.
如果它是较长路径(例如os.path.expanduser('~/some/directory/file.txt')
)的一部分,也可以使用.如果路径中没有~,函数将返回不变的路径.
所以这取决于你想做什么,总比读os.environ['HOME']
强
用户名可通过getpass.getuser()
获得