Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux上使用sshfs装载的文件的Python os.access错误_Python_Linux - Fatal编程技术网

Linux上使用sshfs装载的文件的Python os.access错误

Linux上使用sshfs装载的文件的Python os.access错误,python,linux,Python,Linux,我有以下设置:我有一个安装了sshfs的磁盘,我正试图通过在我的机器上本地运行的python脚本访问磁盘上的文件。问题是,即使我的用户对文件没有任何权限,例如: -rw------- 1 13912 1313 40 Nov 25 11:04 test_file.txt 我不是文件的所有者,os.access仍然返回True: >> os.access('/path/to/file/test_file.txt', os.R_OK) >> True 虽然

我有以下设置:我有一个安装了sshfs的磁盘,我正试图通过在我的机器上本地运行的python脚本访问磁盘上的文件。问题是,即使我的用户对文件没有任何权限,例如:

-rw------- 1 13912 1313          40 Nov 25 11:04 test_file.txt
我不是文件的所有者,os.access仍然返回True:

>> os.access('/path/to/file/test_file.txt', os.R_OK)
>> True
虽然当我试图打开这个文件时,我得到了一个权限被拒绝的错误-这是预期的行为

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

>> IOError: [Errno 13] Permission denied: '/path/to/file/test_file.txt'
有人能解释一下我做错了什么,或者我在os.access方面遗漏了什么吗?是不是因为该文件属于我的操作系统中不存在的用户?
提前谢谢

文档中指出,即使访问表明I/O操作会成功,I/O操作也可能失败,特别是对于网络文件系统上的操作,这些操作的权限语义可能超出了通常的POSIX权限位模型。最好的选择可能是使用try:,除了IOError:。我明白了。我还没有看到那部分文档。问题是没有抛出异常-所以它实际上并没有失败,它只是返回了错误的答案!!!等等,什么时候打印IOError:[Errno 13]权限被拒绝:。。。?如果它正在打印,它应该抛出一个IOError,并且您应该能够捕获它,除了IOError:。因此它只在步骤2抛出错误-当我尝试打开文件时。如上所述,我的脚本有两个步骤:1。检查os.access是否返回True,如果是-步骤2:打开文件并执行smth。问题是第一步没有抛出任何异常,只有第二步会抛出异常——第一步返回True,好像一切正常,所以检查没有用。啊,好的,我明白了。我的意思是,您可以使用try/except语法作为步骤1的替代,因为这似乎超出了os.access的容量。