path.py 文件源码

python
阅读 34 收藏 0 点赞 0 评论 0

项目:scarlett_os 作者: bossjones 项目源码 文件源码
def isWritable(path):
    """Returns whether the file/path is writable."""
    try:
        if os.path.isdir(path):
            # The given path is an existing directory.
            # To properly check if it is writable, you need to use os.access.
            return os.access(path, os.W_OK)
        else:
            # The given path is supposed to be a file.
            # Avoid using open(path, "w"), as it might corrupt existing files.
            # And yet, even if the parent directory is actually writable,
            # open(path, "rw") will IOError if the file doesn't already exist.
            # Therefore, simply check the directory permissions instead:

            # path = 'file:///etc/fstab'
            # In [22]: os.path.dirname(path)
            # Out[22]: 'file:///etc'
            return os.access(os.path.dirname(path), os.W_OK)
            # In [23]: os.access(os.path.dirname(path), os.W_OK)
            # Out[23]: False
    except UnicodeDecodeError:
        unicode_error_dialog()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号