社区所有版块导航
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学习  »  Matt Messersmith  »  全部回复
回复总数  2
5 年前
回复了 Matt Messersmith 创建的主题 » Python:操作系统的函数。访问是不可信的

这与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 .

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

6 年前
回复了 Matt Messersmith 创建的主题 » python中2个range和n个element之间的列表

numpy 做你想做的事:

>>> import numpy as np
>>> np.linspace(0, 2, 5)
array([0. , 0.5, 1. , 1.5, 2. ])

>>> list(np.linspace(0, 2, 5))
[0.0, 0.5, 1.0, 1.5, 2.0]