码迷,mamicode.com
首页 > 其他好文 > 详细

shutil模块

时间:2016-05-29 18:11:51      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

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")复制目录,并把目录下的子目录或文件递归复制到目标目录。

 

shutil模块

标签:

原文地址:http://www.cnblogs.com/owasp/p/5500045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!