Python shutil模組

來源:互聯網
上載者:User

標籤:ref   sys   ike   forbidden   default   likely   getattr   with   nbsp   

 shutil模組

進階的 檔案、檔案夾、壓縮包 處理模組

 copyfileobj

shutil.copyfileobj(fsrc, fdst[, length])
將檔案內容拷貝到另一個檔案中,可以部分內容 

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)   

  

 copyfile

shutil.copyfile(src, dst)
拷貝檔案 

def copyfile(src, dst):    """Copy data from src to dst"""    if _samefile(src, dst):        raise Error("`%s` and `%s` are the same file" % (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)    with open(src, ‘rb‘) as fsrc:        with open(dst, ‘wb‘) as fdst:            copyfileobj(fsrc, fdst)    

 

 copymode

shutil.copymode(src, dst)
僅拷貝許可權。內容、組、使用者均不變

def copymode(src, dst):    """Copy mode bits from src to dst"""    if hasattr(os, ‘chmod‘):        st = os.stat(src)        mode = stat.S_IMODE(st.st_mode)        os.chmod(dst, mode)   

 

 copystat

shutil.copystat(src, dst)
拷貝狀態的資訊,包括:mode bits, atime, mtime, flags

def copystat(src, dst):    """Copy all stat info (mode bits, atime, mtime, flags) from src to dst"""    st = os.stat(src)    mode = stat.S_IMODE(st.st_mode)    if hasattr(os, ‘utime‘):        os.utime(dst, (st.st_atime, st.st_mtime))    if hasattr(os, ‘chmod‘):        os.chmod(dst, mode)    if hasattr(os, ‘chflags‘) and hasattr(st, ‘st_flags‘):        try:            os.chflags(dst, st.st_flags)        except OSError, why:            for err in ‘EOPNOTSUPP‘, ‘ENOTSUP‘:                if hasattr(errno, err) and why.errno == getattr(errno, err):                    break            else:                raise    

 

 copy

shutil.copy(src, dst)
拷貝檔案和許可權

def copy(src, dst):    """Copy data and mode bits ("cp src dst").    The destination may be a directory.    """    if os.path.isdir(dst):        dst = os.path.join(dst, os.path.basename(src))    copyfile(src, dst)    copymode(src, dst)  

 

 copy2

shutil.copy2(src, dst)
拷貝檔案和狀態資訊

def copy2(src, dst):    """Copy data and all stat info ("cp -p src dst").    The destination may be a directory.    """    if os.path.isdir(dst):        dst = os.path.join(dst, os.path.basename(src))    copyfile(src, dst)    copystat(src, dst)  

 

 ignore_patterns/copytree

shutil.ignore_patterns(*patterns)
shutil.copytree(src, dst, symlinks=False, ignore=None)
遞迴的去拷貝檔案

 

例如:copytree(source, destination, ignore=ignore_patterns(‘*.pyc‘, ‘tmp*‘))

def ignore_patterns(*patterns):    """Function that can be used as copytree() ignore parameter.    Patterns is a sequence of glob-style patterns    that are used to exclude files"""    def _ignore_patterns(path, names):        ignored_names = []        for pattern in patterns:            ignored_names.extend(fnmatch.filter(names, pattern))        return set(ignored_names)    return _ignore_patternsdef copytree(src, dst, symlinks=False, ignore=None):    """Recursively copy a directory tree using copy2().    The destination directory must not already exist.    If exception(s) occur, an Error is raised with a list of reasons.    If the optional symlinks flag is true, symbolic links in the    source tree result in symbolic links in the destination tree; if    it is false, the contents of the files pointed to by symbolic    links are copied.    The optional ignore argument is a callable. If given, it    is called with the `src` parameter, which is the directory    being visited by copytree(), and `names` which is the list of    `src` contents, as returned by os.listdir():        callable(src, names) -> ignored_names    Since copytree() is called recursively, the callable will be    called once for each directory that is copied. It returns a    list of names relative to the `src` directory that should    not be copied.    XXX Consider this example code rather than the ultimate tool.    """    names = os.listdir(src)    if ignore is not None:        ignored_names = ignore(src, names)    else:        ignored_names = set()    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(srcname, dstname, symlinks, ignore)            else:                # Will raise a SpecialFileError for unsupported file types                copy2(srcname, dstname)        # catch the Error from the recursive copytree so that we can        # continue with other files        except Error, err:            errors.extend(err.args[0])        except EnvironmentError, why:            errors.append((srcname, dstname, str(why)))    try:        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 Error, errors  

 

 rmtree

shutil.rmtree(path[, ignore_errors[, onerror]])
遞迴的去刪除檔案

def rmtree(path, ignore_errors=False, onerror=None):    """Recursively delete a directory tree.    If ignore_errors is set, errors are ignored; otherwise, if onerror    is set, it is called to handle the error with arguments (func,    path, exc_info) where func is os.listdir, os.remove, or os.rmdir;    path is the argument to that function that caused it to fail; and    exc_info is a tuple returned by sys.exc_info().  If ignore_errors    is false and onerror is None, an exception is raised.    """    if ignore_errors:        def onerror(*args):            pass    elif onerror is None:        def onerror(*args):            raise    try:        if os.path.islink(path):            # symlinks to directories are forbidden, see bug #1669            raise OSError("Cannot call rmtree on a symbolic link")    except OSError:        onerror(os.path.islink, path, sys.exc_info())        # can‘t continue even if onerror hook returns        return    names = []    try:        names = os.listdir(path)    except os.error, err:        onerror(os.listdir, path, sys.exc_info())    for name in names:        fullname = os.path.join(path, name)        try:            mode = os.lstat(fullname).st_mode        except os.error:            mode = 0        if stat.S_ISDIR(mode):            rmtree(fullname, ignore_errors, onerror)        else:            try:                os.remove(fullname)            except os.error, err:                onerror(os.remove, fullname, sys.exc_info())    try:        os.rmdir(path)    except os.error:        onerror(os.rmdir, path, sys.exc_info())  

 

 move

shutil.move(src, dst)
遞迴的去移動檔案

def move(src, dst):    """Recursively move a file or directory to another location. This is    similar to the Unix "mv" command.    If the destination is a directory or a symlink to a directory, the source    is moved inside the directory. The destination path must not already    exist.    If the destination already exists but is not a directory, it may be    overwritten depending on os.rename() semantics.    If the destination is on our current filesystem, then rename() is used.    Otherwise, src is copied to the destination and then removed.    A lot more could be done here...  A look at a mv.c shows a lot of    the issues this implementation glosses over.    """    real_dst = dst    if os.path.isdir(dst):        if _samefile(src, dst):            # We might be on a case insensitive filesystem,            # perform the rename anyway.            os.rename(src, dst)            return        real_dst = os.path.join(dst, _basename(src))        if os.path.exists(real_dst):            raise Error, "Destination path ‘%s‘ already exists" % real_dst    try:        os.rename(src, real_dst)    except OSError:        if os.path.isdir(src):            if _destinsrc(src, dst):                raise Error, "Cannot move a directory ‘%s‘ into itself ‘%s‘." % (src, dst)            copytree(src, real_dst, symlinks=True)            rmtree(src)        else:            copy2(src, real_dst)            os.unlink(src)  

 

 make_archive

shutil.make_archive(base_name, format,...)

建立壓縮包並返迴文件路徑,例如:zip、tar

    • base_name: 壓縮包的檔案名稱,也可以是壓縮包的路徑。只是檔案名稱時,則儲存至目前的目錄,否則儲存至指定路徑,
      如:www =>儲存至當前路徑
      如:/Users/wupeiqi/www =>儲存至/Users/wupeiqi/
    • format:壓縮包種類,“zip”, “tar”, “bztar”,“gztar”
    • root_dir:要壓縮的檔案夾路徑(預設目前的目錄)
    • owner:使用者,預設目前使用者
    • group:組,預設當前組
    • logger:用於記錄日誌,通常是logging.Logger對象
#將 /Users/wupeiqi/Downloads/test 下的檔案打包放置當前程式目錄 import shutilret = shutil.make_archive("wwwwwwwwww", ‘gztar‘, root_dir=‘/Users/wupeiqi/Downloads/test‘)  #將 /Users/wupeiqi/Downloads/test 下的檔案打包放置 /Users/wupeiqi/目錄import shutilret = shutil.make_archive("/Users/wupeiqi/wwwwwwwwww", ‘gztar‘, root_dir=‘/Users/wupeiqi/Downloads/test‘)  

 

def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,                 dry_run=0, owner=None, group=None, logger=None):    """Create an archive file (eg. zip or tar).    ‘base_name‘ is the name of the file to create, minus any format-specific    extension; ‘format‘ is the archive format: one of "zip", "tar", "bztar"    or "gztar".    ‘root_dir‘ is a directory that will be the root directory of the    archive; ie. we typically chdir into ‘root_dir‘ before creating the    archive.  ‘base_dir‘ is the directory where we start archiving from;    ie. ‘base_dir‘ will be the common prefix of all files and    directories in the archive.  ‘root_dir‘ and ‘base_dir‘ both default    to the current directory.  Returns the name of the archive file.    ‘owner‘ and ‘group‘ are used when creating a tar archive. By default,    uses the current owner and group.    """    save_cwd = os.getcwd()    if root_dir is not None:        if logger is not None:            logger.debug("changing into ‘%s‘", root_dir)        base_name = os.path.abspath(base_name)        if not dry_run:            os.chdir(root_dir)    if base_dir is None:        base_dir = os.curdir    kwargs = {‘dry_run‘: dry_run, ‘logger‘: logger}    try:        format_info = _ARCHIVE_FORMATS[format]    except KeyError:        raise ValueError, "unknown archive format ‘%s‘" % format    func = format_info[0]    for arg, val in format_info[1]:        kwargs[arg] = val    if format != ‘zip‘:        kwargs[‘owner‘] = owner        kwargs[‘group‘] = group    try:        filename = func(base_name, base_dir, **kwargs)    finally:        if root_dir is not None:            if logger is not None:                logger.debug("changing back to ‘%s‘", save_cwd)            os.chdir(save_cwd)    return filename  

 

  shutil 對壓縮包的處理是調用 ZipFile 和 TarFile 兩個模組來進行的,詳細:

import zipfile# 壓縮z = zipfile.ZipFile(‘laxi.zip‘, ‘w‘)z.write(‘a.log‘)z.write(‘data.data‘)z.close()# 解壓z = zipfile.ZipFile(‘laxi.zip‘, ‘r‘)z.extractall()z.close()zipfile 壓縮解壓  

 

import tarfile# 壓縮tar = tarfile.open(‘your.tar‘,‘w‘)tar.add(‘/Users/wupeiqi/PycharmProjects/bbs2.zip‘, arcname=‘bbs2.zip‘)tar.add(‘/Users/wupeiqi/PycharmProjects/cmdb.zip‘, arcname=‘cmdb.zip‘)tar.close()# 解壓tar = tarfile.open(‘your.tar‘,‘r‘)tar.extractall()  # 可設定解壓地址tar.close()tarfile 壓縮解壓  

 

Python shutil模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.