file open "no such file"

G

Gary Wessle

I am getting this error when I try to run the code below

****************
f = open("~/m", "r")
print f.read()
****************

****************
:~$ python python/my.py
Traceback (most recent call last):
File "python/my.py", line 1, in ?
f = open("~/m", "r")
IOError: [Errno 2] No such file or directory: '~/m'
****************

but I have the "m" file in my home/username/


thank you
 
R

Robert Kern

Gary said:
I am getting this error when I try to run the code below

****************
f = open("~/m", "r")
print f.read()
****************

****************
:~$ python python/my.py
Traceback (most recent call last):
File "python/my.py", line 1, in ?
f = open("~/m", "r")
IOError: [Errno 2] No such file or directory: '~/m'
****************

but I have the "m" file in my home/username/

There is no automatic ~expansion. You will need to use the function
os.path.expanduser().

--
Robert Kern
(e-mail address removed)

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top