Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.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 - Fatal编程技术网

Python 检查您是否有目录的权限

Python 检查您是否有目录的权限,python,Python,我正在编写一个python程序,现在我正在处理异常 while True: try: os.makedirs("{}\\test".format(dest)) except PermissionError: print("Make sure that you have access to specified path") print("Try again specify your path: ", end='') d

我正在编写一个python程序,现在我正在处理异常

while True:
    try:
        os.makedirs("{}\\test".format(dest))
    except PermissionError:
        print("Make sure that you have access to specified path")
        print("Try again specify your path: ", end='')
        dest = input()
        continue
    break
它正在工作,但稍后我需要删除该文件夹。
更好的方法是什么?

我想你想要的是
os.access

访问(路径,模式,*,目录=None,有效ID=False,跟随符号链接=True)

使用真实uid/gid测试对路径的访问。请注意,大多数操作将使用有效的uid/gid,因此可以在suid/sgid环境中使用此例程来测试调用用户是否具有指定的路径访问权限。模式应为F_OK以测试路径是否存在,也可以是R_OK、W_OK和X_OK中的一个或多个的包含或,以测试权限。如果允许访问,则返回True;如果不允许,则返回False

例如:

os.access(“/path”,os.R\u OK)
模式包括:

os.F#u OK#存在
os.R#U OK#可读性
os.W#正常#可写性
os.X_OK#可执行性
请参阅:

不要

验证您是否具有执行程序所需操作的权限几乎是不值得的。首先,权限不是失败的唯一可能原因。例如,由于另一个程序锁定了文件,删除也可能失败。除非您有很好的理由不这样做,否则只需编写代码来尝试操作,然后在失败时中止,效率更高,可靠性也更高:

import shutil

try:
    shutil.rmtree(path_to_remove) # Recursively deletes directory and files inside it
except Exception as ex:
    print('Failed to delete directory, manual clean up may be required: {}'.format(path_to_remove))
    sys.exit(1)


关于代码的其他问题
  • 使用
    os.path.join
    连接文件路径:
    os.makedirs(os.path.join(dest,test))
    。这将为操作系统使用适当的目录分隔符
  • 你为什么老是说失败?在现实世界的程序中,简单地中止整个操作更简单,通常会带来更好的用户体验
  • 你确定你不是在找图书馆吗?它允许您向操作系统的标准临时位置吐出一个唯一的目录:

    import tempfile
    
    with tempfile.TemporaryDirectory() as tmpdir:
        some_function_that_creates_several_files(tmpdir)
        for f in os.walk(tmpdir):
             # do something with each file
    # tmpdir automatically deleted when context manager exits
    
    # Or if you really only need the file
    with tempfile.TemporaryFile() as tmpfile:
        tmpfile.write('my data')
        some_function_that_needs_a_file(tmpfile)
    # tmpfile automatically deleted when context manager exits
    

您是否尝试过
os.rmdir(path)
?但我不想更改此目录中的任何内容,只想检查我是否有权限查看文档,它说您不应该出于安全目的执行示例。但是另一个(在文档后面)。*@IMCoins谢谢你指出我的错误,我已经修改了这个例子。@maxmarsz很抱歉我不能完全理解你想要什么。100%同意。在Python语言中,请求原谅比请求允许(EAFP)更容易,而不是三思而后行(LBYL)。实际上,在检查许可和实际尝试使用许可之间,事情经常会发生变化。在繁忙的系统上,LBYL有太多的潜在竞争条件,无法成为安全模式。@KirkStrauser在不繁忙的系统上,您不太可能有问题,因为脚本创建了要删除的目录。@maxmarsz用户希望在出现问题时需要再次运行程序。无限循环有一个主要的缺点,即当试图自动执行某些操作时,从另一个脚本调用脚本不再安全。@maxmarsz我认为最好将错误告知用户,中止操作(如果这是终端脚本,则退出程序并带有错误代码,如果是GUI,则返回界面的其他部分),然后让用户决定下一步要做什么。用户可能决定更改输入并重试,或者他们可能决定暂时放弃,稍后再试。这取决于他们。这就是现实世界的程序通常的行为方式。@maxmarsz正如我所说的,你没有。您只需尝试创建文件或文件夹。如果您的程序实际上没有对该位置执行任何操作,那么它实际上不需要权限,也不应该检查它们。