【发布时间】:2021-10-03 06:41:41
【问题描述】:
我对以下 python 代码有疑问
import os
print("Can I write into the tv folder?",os.access("/tv", os.W_OK))
print("Does the /tv/test file exists?", os.access("/tv/test", os.R_OK))
with open("/tv/test", "w") as f:
f.write("toto")
print("Does the /tv/test file exists now?", os.access("/tv/test", os.R_OK))
with open("/tv/test", "r") as f:
print("Content of the /tv/test file:")
print(f.read())
打印出来
Can I write into the tv folder? False
Does the /tv/test file exists? False
Does the /tv/test file exists now? True
Content of the /tv/test file;
toto
但根据第一次调用os.access,我应该无法编写测试文件...
我的问题不在于它已经能够创建文件,我想要那个。但是测试应该产生相关的结果。 (我使用的一个软件因此而出现故障)
我在树莓派上的 docker 容器(Docker 版本 20.10.7)中运行 Python 3.8.10,tv 文件夹是 docker 挂载的远程 nfs 共享。
root@7f0a44aad8a9:/> ls -la /tv
total 8
drwxrwxrwx 1 abc abc 3826 Jul 27 14:18 .
drwxr-xr-x 1 root root 4096 Jul 27 14:12 ..
drwxrwxrwx 1 abc abc 988 May 13 07:30 DARK
... (a lot of other folders)
-rwxrwxrwx 1 abc abc 4 Jul 27 14:18 test
我的 docker-compose 文件:
version: "3.4"
services:
bazarr:
image: ghcr.io/linuxserver/bazarr
container_name: bazarr
volumes:
- shows:/tv
ports:
- 6767:6767
dns: 1.1.1.1
restart: unless-stopped
volumes:
shows:
driver_opts:
type: "nfs"
o: "nfsvers=3,addr=xxx,soft,rw"
device: ":xxx"
我知道os.access 可以给出假阳性,但可以给出假阴性?
有人看过这个吗?
这是意料之中的吗?
编辑:问题不是特定于 python 的
root@6c5dd99ee211:/> if [ -w /tv ]; then echo "WRITABLE"; else echo "NOT WRITABLE"; fi
NOT WRITABLE
但是为什么呢?
【问题讨论】:
-
可以
ls -la /tv吗? -
您是否确认您的问题实际上是 Python 代码而不是底层操作?比如
test -w /tv在shell中成功了吗? -
你是对的,
test -w /tv失败了。但为什么 ?因为我确实有权在里面创建文件。
标签: python linux docker file-permissions nfs