def duplicate(src, dst):
"""Alternative to shutil.copyfile, this will use os.link when possible.
See shutil.copyfile for documention. Only `src` and `dst` are supported.
Unlike copyfile, this will not overwrite the destination if it exists.
"""
if os.path.isdir(src):
# os.link will give a permission error
raise OSError(errno.EISDIR, "Is a directory", src)
if os.path.isdir(dst):
# os.link will give a FileExists error
raise OSError(errno.EISDIR, "Is a directory", dst)
if os.path.exists(dst):
# shutil.copyfile will overwrite the existing file
raise OSError(errno.EEXIST, "File exists", src, "File exists", dst)
try:
os.link(src, dst)
except OSError as e:
if e.errno == errno.EXDEV: # Invalid cross-device link
shutil.copyfile(src, dst)
else:
raise
评论列表
文章目录