在Python中测试目录权限?

发布于 2024-07-13 08:39:06 字数 371 浏览 7 评论 0原文

在Windows上的Python中,有没有办法确定用户是否有权访问目录? 我查看了 os.access,但它给出了错误的结果。

>>> os.access('C:\haveaccess', os.R_OK)
False
>>> os.access(r'C:\haveaccess', os.R_OK)
True
>>> os.access('C:\donthaveaccess', os.R_OK)
False
>>> os.access(r'C:\donthaveaccess', os.R_OK)
True

难道我做错了什么? 有没有更好的方法来检查用户是否有权访问目录?

In Python on Windows, is there a way to determine if a user has permission to access a directory? I've taken a look at os.access but it gives false results.

>>> os.access('C:\haveaccess', os.R_OK)
False
>>> os.access(r'C:\haveaccess', os.R_OK)
True
>>> os.access('C:\donthaveaccess', os.R_OK)
False
>>> os.access(r'C:\donthaveaccess', os.R_OK)
True

Am I doing something wrong? Is there a better way to check if a user has permission to access a directory?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

在 Windows 中检查权限可能很复杂(例如,请注意 Vista 中 UAC 的问题! - 请参阅此 相关问题)。

您是在谈论简单的读取访问,即读取目录的内容吗?
测试权限的最可靠方法是尝试访问目录(例如执行os.listdir)并捕获异常。

另外,为了正确解释路径,您必须使用原始字符串或转义反斜杠('\\'),--或使用正斜杠。

(编辑:您可以使用 os.path.join 来完全避免斜杠——这是构建路径的推荐方法)

It can be complicated to check for permissions in Windows (beware of issues in Vista with UAC, for example! -- see this related question).

Are you talking about simple read access, i.e. reading the directory's contents?
The surest way of testing permissions would be to try to access the directory (e.g. do an os.listdir) and catch the exception.

Also, in order for paths to be interpreted correctly you have to use raw strings or escape the backslashes ('\\'), -- or use forward slashes instead.

(EDIT: you can avoid slashes altogether by using os.path.join -- the recommended way to build paths)

归途 2024-07-20 08:39:06

虽然 os.access 尽力判断路径是否可访问,但它并不声称是完美的。 来自Python文档:

注意:I/O 操作甚至可能失败
当 access() 表明他们
将会成功,特别是对于
网络文件系统上的操作
可能有权限语义
超出通常的 POSIX 权限位
型号。

确定用户是否有权执行任何操作的推荐方法是尝试执行该操作,并捕获发生的任何异常。

While os.access tries its best to tell if a path is accessible or not, it doesn't claim to be perfect. From the Python docs:

Note: I/O operations may fail even
when access() indicates that they
would succeed, particularly for
operations on network filesystems
which may have permissions semantics
beyond the usual POSIX permission-bit
model.

The recommended way to find out if the user has access to do whatever is to try to do it, and catch any exceptions that occur.

剧终人散尽 2024-07-20 08:39:06

实际上 'C:\haveaccess' 与 r'C:\haveaccess' 不同。
从 Python 的角度来看,'C:\haveaccess' 不是有效路径,因此请使用 'C:\\haveaccess' 代替。
我认为 os.access 工作得很好。

Actually 'C:\haveaccess' is different than r'C:\haveaccess'.
From Python point of view 'C:\haveaccess' is not a valid path, so use 'C:\\haveaccess' instead.
I think os.access works just fine.

情痴 2024-07-20 08:39:06

我将提供@dF.使用 os.scandir/os.listdirEAFP

def can_access(path: str) -> bool:
    """Check if we can access folder on network drive"""
    try:
        os.scandir(path)
        return True
    except PermissionError:
        return False

I will provide code for @dF.'s approach of using os.scandir/os.listdir with EAFP:

def can_access(path: str) -> bool:
    """Check if we can access folder on network drive"""
    try:
        os.scandir(path)
        return True
    except PermissionError:
        return False
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文