Reputation: 16469
If I am in the directory ~/a/b/c/d
. I want a program called hithere.py
and in that program I want to pass some input that is found in file hi.txt
and is located in directory b. How would I do so?
I tried f = open("~/a/b/hi.txt","r")
but I get the error No such file or directory
Upvotes: 0
Views: 113
Reputation: 948
Tilde is bash stuff - you can't use it without bash. So try something like
f = open("../../hi.txt","r")
Upvotes: 2
Reputation: 5971
You need to use os.path.expanduser()
, as python doesn't automatically expand paths, so try this:
import os
f = open(os.path.expanduser('~/a/b/hi.txt'), 'r')
Upvotes: 4