shutil模塊

shutil.copyfileobj()文件拷貝,只拷貝文件內容:python

# 文件文件拷貝
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"打開,也就是無論目標存不存在,都會生成新的目標文件,不能複製目錄socket

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),拷貝文件權限this

"""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):拷貝文件權限、訪問時間和標識orm

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): 拷貝文件和文件權限,不能複製目錄blog

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")複製目錄,並把目錄下的子目錄或文件遞歸複製到目標目錄。ip

相關文章
相關標籤/搜索