Skip to content

Noob error in Python opening a file

An answer to this question on Stack Overflow.

Question

I am placing this code into IDLE:

f = open('/Users/alex/Documents/URM8/health.tdf')

I don't understand why I am unable to open it. I get the error:

Traceback (most recent call last):
  File "<pyshell#21>", line 1, in <module>
    f = open('/Users/alex/Documents/URM8/health.tdf')
IOError: [Errno 2] No such file or directory: '/Users/alex/Documents/URM8/health.tdf'

Of course usually the problem is filename. I have checked it lots of times and it is correct.

I 'unlocked' the file (I'm using Mac OSX). Also set write access to Everyone in Mac OSX.

Do i need to set permissions in Bash?

Really appreciate someone telling me what I'm doing wrong!

Answer

Are you trying to use a relative path? The leading '/' could be a problem.

You don't specifically define the file mode ("r", "w", etc.) in your open call, you may want to reconsider this.

You could try ls -l on the file to get its permissions. chmod u+rw <FILE> should give you access.