Skip to content

Instantly share code, notes, and snippets.

@ladder1984
Created December 21, 2015 09:40
Show Gist options
  • Save ladder1984/a063d3f1ca0d9be57a81 to your computer and use it in GitHub Desktop.
Save ladder1984/a063d3f1ca0d9be57a81 to your computer and use it in GitHub Desktop.
修改自标准库的copytree,可覆盖文件
# -*- coding: utf-8 -*-
# 修改自标准库的copytree,可覆盖文件
import os
import shutil
def copytree_override(src, dst, symlinks=False, ignore=None):
names = os.listdir(src)
if ignore is not None:
ignored_names = ignore(src, names)
else:
ignored_names = set()
if not os.path.isdir(dst):
os.makedirs(dst)
errors = []
for name in names:
if name in ignored_names:
continue
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if symlinks and os.path.islink(srcname):
linkto = os.readlink(srcname)
os.symlink(linkto, dstname)
elif os.path.isdir(srcname):
copytree_override(srcname, dstname, symlinks, ignore)
else:
# Will raise a SpecialFileError for unsupported file types
shutil.copy2(srcname, dstname)
# catch the Error from the recursive copytree so that we can
# continue with other files
except shutil.Error, err:
errors.extend(err.args[0])
except EnvironmentError, why:
errors.append((srcname, dstname, str(why)))
try:
shutil.copystat(src, dst)
except OSError, why:
if WindowsError is not None and isinstance(why, WindowsError):
# Copying file access times may fail on Windows
pass
else:
errors.append((src, dst, str(why)))
if errors:
raise shutil.Error, errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment