def check_path(path, ptype=None, access=None):
"""Checks that a path exists, is of the specified type, and allows the
specified access.
Args:
ptype: 'f' for file or 'd' for directory.
access (int): One of the access values from :module:`os`
Raises:
IOError if the path does not exist, is not of the specified type, or
doesn't allow the specified access.
"""
if (
ptype == 'f' and not path.startswith("/dev/") and
not os.path.isfile(path)):
raise IOError(errno.EISDIR, "{} is not a file".format(path), path)
elif ptype == 'd' and not os.path.isdir(path):
raise IOError(errno.ENOTDIR, "{} is not a directory".format(path), path)
elif not os.path.exists(path):
raise IOError(errno.ENOENT, "{} does not exist".format(path), path)
if access is not None and not os.access(path, access):
raise IOError(errno.EACCES, "{} is not accessable".format(path), path)
return path
评论列表
文章目录