[英]Is there a way I can use ~ to point to the home directory in file paths in Python?
我试图用Python(版本3.5,但我也对2.7很好奇)而不是Shell脚本来自动化一些任务。 如果我能写"~/myfile.txt"
而不是/home/me/myfile.txt
,我会感到很高兴。 这可能吗?
使用os.path.expanduser("~")
for line in open(os.path.expanduser("~/my_file")):
print line
文件:
在Unix和Windows上,返回带有〜或〜user初始组成部分的参数替换为该用户的主目录。
https://docs.python.org/2/library/os.path.html#os.path.expanduser
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.