我想使用Python编辑~/.bash_profile,但是当我运行这些代码时:
f = open('~/.bash_profile', 'rb')它告诉我:
IOError: [Errno 2] No such file or directory: '~/.bash_profile'我认为这是因为~/.bash_profile是一个系统保护文件。有任何方法可以使用Python打开这个文件吗?
发布于 2014-11-08 15:15:20
通常,~是由shell展开的,而不是通过系统调用来扩展的。在Python中,使用os.path.expanduser()扩展以~开头的路径。
https://stackoverflow.com/questions/26818513
复制相似问题