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()
评论列表
文章目录