社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

Python:操作系统的函数。访问是不可信的

pums974 • 4 年前 • 1840 次点击  

我对下面的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 ,我不应该写测试文件。。。

我的问题不是它已经能够创建文件,而是我想要它。但测试应该会产生相关的结果。(我使用的一个软件因此出现故障)

我在raspberry pi上的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撰写文件:

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"

我知道 操作系统。通道 可以给出假阳性,但是假阴性?
有人看过吗?
这是意料之中的事吗?

编辑:问题不是特定于python的

root@6c5dd99ee211:/> if [ -w /tv ]; then echo "WRITABLE"; else echo "NOT WRITABLE"; fi
NOT WRITABLE

但为什么呢?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/127906
文章 [ 1 ]  |  最新文章 4 年前
Matt Messersmith
Reply   •   1 楼
Matt Messersmith    5 年前

这与POSIX文件系统(包括*nix文件系统)的关系比与Python的关系要大得多。

但是根据对os的第一次调用。access,我不应该写测试文件。。。

不,不幸的是,这是错误的(但必须承认,这有点不直观)。

在POSIX文件系统中,您可能没有写入目录的权限(这是您所检查的),但是 没有 暗示您无法写入该目录中的文件。这意味着你不能写作 刚出现的 将文件发送到所述目录。

例子:

(base) mmessersmith@HP-ZBK-0425:~$ mkdir test_dir
(base) mmessersmith@HP-ZBK-0425:~$ touch test_dir/abc.txt
(base) mmessersmith@HP-ZBK-0425:~$ cat test_dir/abc.txt
(base) mmessersmith@HP-ZBK-0425:~$ chmod 777 test_dir/abc.txt
(base) mmessersmith@HP-ZBK-0425:~$ chmod 500 test_dir
(base) mmessersmith@HP-ZBK-0425:~$ touch test_dir/new_file.txt
touch: cannot touch 'test_dir/new_file.txt': Permission denied
(base) mmessersmith@HP-ZBK-0425:~$ echo "123" > test_dir/abc.txt
(base) mmessersmith@HP-ZBK-0425:~$ cat test_dir/abc.txt
123
(base) mmessersmith@HP-ZBK-0425:~$ ls -al test_dir/
total 12
dr-x------  2 mmessersmith mmessersmith 4096 Jul 27 09:32 .
drwxr-xr-x 24 mmessersmith mmessersmith 4096 Jul 27 09:31 ..
-rwxrwxrwx  1 mmessersmith mmessersmith    4 Jul 27 09:32 abc.txt

请注意,我无法将新文件写入目录 test_dir ,但我仍然可以写信给 abc.txt 在内部 测试目录 .在上面的例子中,你需要检查 os.access("/tv/test", os.W_OK) .在我对上述示例的回复中:

(base) mmessersmith@HP-ZBK-0425:~$ python
Python 3.8.3 (default, May 19 2020, 18:47:26)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.access('test_dir', os.W_OK)
False
>>> os.access('test_dir/abc.txt', os.R_OK)
True
>>> os.access('test_dir/abc.txt', os.W_OK)
True

最后,请注意 os.access 是linux/unix访问的简单包装器: https://manpages.debian.org/buster/manpages-dev/access.2.en.html .

所以不,这不是假阳性或假阴性,这只是文件系统的工作方式。