这与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
.
所以不,这不是假阳性或假阴性,这只是文件系统的工作方式。