我试图用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