1

I want to use Python to edit ~/.bash_profile, but when I run these codes:

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. Is there any method to open this file by using Python?

1 Answer 1

6

Normally ~ is expanded by the shell, not by the system call. In Python, use os.path.expanduser() to expand paths starting with ~.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.