检查目录是否具有读/写权限

我的环境是运行在 Windows 10 上的 Python 3.7.2

我正在开发一些下载器。首先,我要检查要在其中下载文件的目录是否具有读/写权限:

我尝试了两种方法:

1个代码:

使用os模块(os.access函数)检查访问权限,即使我没有写访问权限也返回True。模块在Windows上给出错误的结果:

# check for read/write access
if not os.access(path,os.W_OK):
    raise PermissionError("Path %s,doesn't have WRITE permissions" % path)
# check for write access
if not os.access(path,os.R_OK):
     raise PermissionError("Path %s,doesn't have READ permissions" % path)

2个代码:

使用tempfile.TemporaryFile()的想法似乎是检查目录权限的一种干净方法,而不会在系统上留下垃圾文件。问题是,当给tempfile.TemporaryFile()赋予没有任何权限的dir参数时,脚本将挂起,脚本将不会继续执行或退出。什么也不会返回。它只是停留在那条线上。该错误仅影响Windows环境,但不幸的是,此错误导致呈现tempfile.TemporaryFile在Windows上无法用于以下常见用例:

import tempfile
import errno

try:
    testfile = tempfile.TemporaryFile(dir = path)
    testfile.close()
except (OSError,IOError) as e:
    if e.errno == errno.EaccES or e.errno == errno.EEXIST:
        raise PermissionError("Path %s,doesn't have READ/WRITE permissions" % path)

任何人都知道如何检查目录的权限

shuojie 回答:检查目录是否具有读/写权限

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3073817.html

大家都在问