简体   繁体   English

如何使用Python打开“〜/ .bash_profile”

[英]How can I use Python to open “~/.bash_profile”

I want to use Python to edit ~/.bash_profile , but when I run these codes: 我想使用Python编辑~/.bash_profile ,但是当我运行这些代码时:

f = open('~/.bash_profile', 'rb')

it told me: 它告诉我:

IOError: [Errno 2] No such file or directory: '~/.bash_profile'

I thought it was because ~/.bash_profile is a system protect file. 我以为是因为~/.bash_profile是系统保护文件。 Is there any method to open this file by using Python? 是否有使用Python打开此文件的方法?

Normally ~ is expanded by the shell, not by the system call. 通常~由外壳扩展,而不是由系统调用扩展。 In Python, use os.path.expanduser() to expand paths starting with ~ . 在Python中,使用os.path.expanduser()扩展以~开头的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM