Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检查目录是否具有读/写权限_Python_Directory - Fatal编程技术网

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

Python 检查目录是否具有读/写权限,python,directory,Python,Directory,我的环境是运行在Windows10上的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,

我的环境是运行在Windows10上的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)
任何人都知道如何检查目录的权限

,第二个想法很清楚,可以将下载的数据包保存在临时文件夹中,程序创建错误并向用户询问新位置,一旦用户提供了一个有效位置,就可以将包组装在一起,您可以看到此包管理器实现,第二个将是明确的想法,可以保存在临时文件夹下载的数据包,程序创建一个错误,并要求用户一个新的位置,一旦用户提供了一个有效的位置组装在一起的包,你可以看到这个包管理器的实施