Check the permissions of a file in python

前端 未结 1 1356
醉话见心
醉话见心 2021-01-06 04:35

I\'m trying to check the readability of a file given the specified path. Here\'s what I have:

def read_permissions(filepath):
    \'\'\'Checks the read permi         


        
1条回答
  •  礼貌的吻别
    2021-01-06 05:30

    os.access returns False when the file does not exist, regardless of the mode parameter passed.

    This isn't stated explicitly in the documentation for os.access but it's not terribly shocking behavior; after all, if a file doesn't exist, you can't possibly access it. Checking the access(2) man page as suggested by the docs gives another clue, in that access returns -1 in a wide variety of conditions. In any case, you can simply do as I did and check the return value in IDLE:

    >>> import os
    >>> os.access('does_not_exist.txt', os.R_OK)
    False
    

    In Python it's generally discouraged to go around checking types and such before trying to actually do useful things. This philosophy is often expressed with the initialism EAFP, which stands for Easier to Ask Forgiveness than Permission. If you refer to the docs again, you'll see this is particularly relevant in the present case:

    Note: Using access() to check if a user is authorized to e.g. open a file before actually doing so using open() creates a security hole, because the user might exploit the short time interval between checking and opening the file to manipulate it. It’s preferable to use EAFP techniques. For example:

    if os.access("myfile", os.R_OK):
        with open("myfile") as fp:
            return fp.read()
    return "some default data"
    

    is better written as:

    try:
        fp = open("myfile")
    except IOError as e:
        if e.errno == errno.EACCES:
            return "some default data"
        # Not a permission error.
        raise
    else:
        with fp:
            return fp.read()
    

    If you have other reasons for checking permissions than second-guessing the user before calling open(), you could look to How do I check whether a file exists using Python? for some suggestions. Remember that if you really need an exception to be raised, you can always raise it yourself; no need to go hunting for one in the wild.


    Since the IOError can be brought up a couple different ways (such as permission denied, or non-existent directory), I am trying to get my module to identify and publish the issue.

    That's what the second approach above does. See:

    >>> try:
    ...     open('file_no_existy.gif')
    ... except IOError as e:
    ...     pass
    ...
    >>> e.args
    (2, 'No such file or directory')
    >>> try:
    ...     open('unreadable.txt')
    ... except IOError as e:
    ...     pass
    ...
    >>> e.args
    (13, 'Permission denied')
    >>> e.args == (e.errno, e.strerror)
    True
    

    But you need to pick one approach or the other. If you're asking forgiveness, do the thing (open the file) in a try-except block and deal with the consequences appropriately. If you succeed, then you know you succeeded because there's no exception.

    On the other hand, if you ask permission (aka LBYL, Look Before You Leap) in this that and the other way, you still don't know if you can successfully open the file until you actually do it. What if the file gets moved after you check its permissions? What if there's a problem you didn't think to check for?

    If you still want to ask permission, don't use try-except; you're not doing the thing so you're not going to throw errors. Instead, use conditional statements with calls to os.access as the condition.

    0 讨论(0)
提交回复
热议问题