shutil模块
shutil.copyfileobj()文件拷贝,只拷贝文件内容:
# 文件文件拷贝 f1 = open("srcfile",'r') f2 = open("dstfile",'w') shutil.copyfileobj(f1,f2) # 二进制文件拷贝 f1 = open("srcfile_b.mp4",'rb') f2 = open("dstfile_b.mp4",'wb') shutil.copyfileobj(f1,f2) # copyfileobj源码: def copyfileobj(fsrc, fdst, length=16*1024): """copy data from file-like object fsrc to file-like object fdst""" while 1: buf = fsrc.read(length) if not buf: break fdst.write(buf)
shutil.copyfile(src,dst)拷贝文件,参数是文件路径。目标文件是已"wb"打开,也就是不管目标存不存在,都会生成新的目标文件,不能复制目录
shutil.copyfile("srcfile","dstfile") # copyfile源代码 def copyfile(src, dst, *, follow_symlinks=True): """Copy data from src to dst. If follow_symlinks is not set and src is a symbolic link, a new symlink will be created instead of copying the file it points to. """ if _samefile(src, dst): raise SameFileError("{!r} and {!r} are the same file".format(src, dst)) for fn in [src, dst]: try: st = os.stat(fn) except OSError: # File most likely does not exist pass else: # XXX What about other special files? (sockets, devices...) if stat.S_ISFIFO(st.st_mode): raise SpecialFileError("`%s` is a named pipe" % fn) if not follow_symlinks and os.path.islink(src): os.symlink(os.readlink(src), dst) else: with open(src, 'rb') as fsrc: with open(dst, 'wb') as fdst: copyfileobj(fsrc, fdst) return dst
copymode(src, dst, *, follow_symlinks=True),拷贝文件权限
"""Copy mode bits from src to dst.
If follow_symlinks is not set, symlinks aren't followed if and only
if both `src` and `dst` are symlinks. If `lchmod` isn't available
(e.g. Linux) this method does nothing.
def copystat(src, dst, *, follow_symlinks=True):拷贝文件权限、访问时间和标识
def copystat(src, dst, *, follow_symlinks=True):
"""Copy all stat info (mode bits, atime, mtime, flags) from src to dst.
If the optional flag `follow_symlinks` is not set, symlinks aren't followed if and
only if both `src` and `dst` are symlinks.
"""
def copy(src, dst, *, follow_symlinks=True): 拷贝文件和文件权限,不能复制目录
def copy(src, dst, *, follow_symlinks=True): """Copy data and mode bits ("cp src dst"). Return the file's destination. The destination may be a directory. If follow_symlinks is false, symlinks won't be followed. This resembles GNU's "cp -P src dst". If source and destination are the same file, a SameFileError will be raised. """ if os.path.isdir(dst): dst = os.path.join(dst, os.path.basename(src)) copyfile(src, dst, follow_symlinks=follow_symlinks) copymode(src, dst, follow_symlinks=follow_symlinks) return dst
def copy2(src, dst, *, follow_symlinks=True):拷贝文件、权限、访问时间,不能复制目录
def copy2(src, dst, *, follow_symlinks=True): """Copy data and all stat info ("cp -p src dst"). Return the file's destination." The destination may be a directory. If follow_symlinks is false, symlinks won't be followed. This resembles GNU's "cp -P src dst". """ if os.path.isdir(dst): dst = os.path.join(dst, os.path.basename(src)) copyfile(src, dst, follow_symlinks=follow_symlinks) copystat(src, dst, follow_symlinks=follow_symlinks) return dst
shutil.copytree("srcdir","dstdir")复制目录,并把目录下的子目录或文件递归复制到目标目录。