Test directory permissions in Python?

前端 未结 3 1327
北荒
北荒 2020-12-07 01:44

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.

3条回答
  •  隐瞒了意图╮
    2020-12-07 01:56

    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.

提交回复
热议问题