0

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!

Alex
  • 4,844
  • 7
  • 44
  • 58
  • 2
    Triple check that the directory and filename is correct (e.g. with `ls`). In case you have a non-default case sensitive HFS filesystem, also check the case. – miku Jun 26 '11 at 19:46
  • 1
    @AlexW Can You give us the output of `ls -l /Users/alex/Documents/URM8/health.tdf` in the terminal? – phihag Jun 26 '11 at 19:46
  • 1
    What about `cat /Users/alex/Documents/URM8/health.tdf` or `ls /Users/alex/Documents/URM8/health.tdf` ? – pajton Jun 26 '11 at 19:46
  • @pajton `cat` is likely to produce a mess in the terminal if the file's binary and therefore not a good idea ;) – phihag Jun 26 '11 at 19:47
  • 1
    @phihag Still we could see if it can be opened for reading. – pajton Jun 26 '11 at 19:48
  • @pajton Well, replacing `cat` with **`file`** results in a neat `not found` or `no read permissions`. – phihag Jun 26 '11 at 19:53
  • Guys thanks for the help - Mac OSX is hiding the .txt extension.. :/ So it is, once again, a filename problem – Alex Jun 26 '11 at 19:53
  • @AlexW: please watch your language, this is not a place to swear at people. – Lekensteyn Jun 26 '11 at 20:16
  • lol - it's annoying when people make stupid mistakes – Alex Jun 26 '11 at 20:22

3 Answers3

2

/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.

phihag
  • 278,196
  • 72
  • 453
  • 469
1

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.

kindall
  • 178,883
  • 35
  • 278
  • 309
  • 1
    Of course I hadn't figured about it hiding the .txt extension. I have all extensions visible normally, but in this case Mac hides .txt `xyz.tdf.txt` because of my Text Editor – Alex Jun 26 '11 at 19:55
0

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.

Richard
  • 56,349
  • 34
  • 180
  • 251
  • 1
    Default file mode is `r`, so even though it should be made explicit, that won't be the problem. – miku Jun 26 '11 at 19:50