Alex
Alex

Reputation: 4934

Noob error in Python opening a file

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!

Upvotes: 0

Views: 225

Answers (3)

phihag
phihag

Reputation: 288300

/Users/alex/Documents/URM8/health.tdf cannot be opened because it's not there; the Mac OS UI hides the .txt extension. open('/Users/alex/Documents/URM8/health.tdf.txt') works fine.

Upvotes: 2

Richard
Richard

Reputation: 61539

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.

Upvotes: 0

kindall
kindall

Reputation: 184455

The problem is not the permissions. If it were, the error message would be different. Is some component of the path a Mac alias to a directory, rather than a directory? If so, Python won't follow it, and will give that error.

Try individual parts of the pathname to see exactly which directory or file Python can't find. You could do this simply using cd in the shell.

Upvotes: 1

Related Questions