2

I have the following set-up: I have a disk mounted with sshfs and I am trying to access the files on it from a python script run locally on my machine. The problem is that even though my user doesn't have any rights over a file - i.e.:

-rw------- 1 13912 1313          40 Nov 25 11:04 test_file.txt

(I am not the owner of the file), os.access still returns True:

>> os.access('/path/to/file/test_file.txt', os.R_OK)
>> True

though when I try to open this file, I get a permission denied error - which is the expected behaviour.

>> f=open('/path/to/file/test_file.txt')

>> IOError: [Errno 13] Permission denied: '/path/to/file/test_file.txt'

Can anyone explain me what am I doing wrong or if there is something I am missing regarding os.access? Is it because this file is owned by a user which doesn't exist in my os? () Thanks in advance!

Clara
  • 2,935
  • 6
  • 34
  • 49
  • The documentation has a note that "I/O operations may fail even when access() indicates that they would succeed, particularly for operations on network filesystems which may have permissions semantics beyond the usual POSIX permission-bit model." The best option is probably to use "try:"/"except IOError:". – seaotternerd Nov 25 '13 at 12:16
  • I see. I haven't seen that part of the documentation. The thing is that there is no exception thrown - so it's not actually failing, it just returns the wrong answer!!! – Clara Nov 25 '13 at 13:12
  • Wait, so when is it printing "IOError: [Errno 13] Permission denied:..."? If it's printing that it should be throwing an IOError, and you should be able to catch it with "except IOError:". – seaotternerd Nov 25 '13 at 16:47
  • So it throws the error only at step 2 - when I am trying to open the file. As described above, my script has 2 steps: 1. check if os.access() returns True, if so -- step 2: open() the file and do smth. The problem is that step 1 doesn't throw any exception, only step 2 does - (step 1 returns returns True, as if everything were ok), so the check is useless. – Clara Nov 25 '13 at 16:52
  • Ah, okay, I see. I meant that you could use the try/except syntax as an alternative to step 1, since this is seemingly beyond the capacity of os.access(). – seaotternerd Nov 26 '13 at 14:20

0 Answers0