Python3 's Shutil module

Source: Internet
Author: User

I. INTRODUCTION

Shutil is an advanced file, folder, and compression package processing module.

Two. Use

Shutil.copyfileobj (FSRC, fdst[, length])
Copy the contents of a file to another file

123 importshutil shutil.copyfileobj(open(‘old.xml‘,‘r‘), open(‘new.xml‘‘w‘))

Shutil.copyfile (SRC, DST)
Copy files

1 shutil.copyfile(‘f1.log‘‘f2.log‘)

Shutil.copymode (SRC, DST)
Copy permissions only. Content, group, user are not changed

1 shutil.copymode(‘f1.log‘‘f2.log‘)

Shutil.copystat (SRC, DST)
Copy only the status information, including: mode bits, Atime, mtime, Flags

1 shutil.copystat(‘f1.log‘‘f2.log‘)

Shutil.copy (SRC, DST)
Copying files and Permissions

1 shutil.copy(‘f1.log‘‘f2.log‘)

Shutil.copy2 (SRC, DST)
Copying files and status information

1 shutil.copy2(‘f1.log‘‘f2.log‘)

Shutil.ignore_patterns (*patterns)
Shutil.copytree (SRC, DST, Symlinks=false, Ignore=none)
Recursive go-to-copy folder

123 shutil.copytree(‘folder1‘‘folder2‘, ignore=shutil.ignore_patterns(‘*.pyc‘‘tmp*‘))shutil.copytree(‘f1‘‘f2‘, symlinks=True, ignore=shutil.ignore_patterns(‘*.pyc‘‘tmp*‘))

Shutil.rmtree (path[, ignore_errors[, OnError])
To delete a file recursively

1 shutil.rmtree(‘folder1‘)

Shutil.move (SRC, DST)
To move a file recursively, it is like the MV command, which is actually renamed.

1 shutil.move(‘folder1‘‘folder3‘)

Shutil.make_archive (base_name, Format,...)

Create a compressed package and return the file path, for example: Zip, tar

Create a compressed package and return the file path, for example: Zip, tar

    • Base_name: The file name of the compressed package, or the path to the compressed package. Only the file name is saved to the current directory, otherwise saved to the specified path,
      For example: www + = Save to Current path
      such as:/users/wupeiqi/www = Save to/users/wupeiqi/
    • Format: Compressed package type, "Zip", "tar", "Bztar", "Gztar"
    • Root_dir: Folder path to compress (default current directory)
    • Owner: User, default Current user
    • Group: Groups, default current group
    • Logger: Used to log logs, usually logging. Logger Object
12345678 #将 /Users/wupeiqi/Downloads/test 下的文件打包放置当前程序目录importshutilret =shutil.make_archive("wwwwwwwwww"‘gztar‘, root_dir=‘/Users/wupeiqi/Downloads/test‘)    #将 /Users/wupeiqi/Downloads/test 下的文件打包放置 /Users/wupeiqi/目录importshutilret =shutil.make_archive("/Users/wupeiqi/wwwwwwwwww"‘gztar‘, root_dir=‘/Users/wupeiqi/Downloads/test‘)

  

Shutil Processing of compressed packets is done by calling the ZipFile and Tarfile two modules.

123456789101112 importzipfile# 压缩=zipfile.ZipFile(‘laxi.zip‘‘w‘)z.write(‘a.log‘)z.write(‘data.data‘)z.close()# 解压=zipfile.ZipFile(‘laxi.zip‘‘r‘)z.extractall()z.close()
123456789101112 importtarfile# 压缩tar =tarfile.open(‘your.tar‘,‘w‘)tar.add(‘/Users/wupeiqi/PycharmProjects/bbs2.log‘, arcname=‘bbs2.log‘)tar.add(‘/Users/wupeiqi/PycharmProjects/cmdb.log‘, arcname=‘cmdb.log‘)tar.close()# 解压tar = tarfile.open(‘your.tar‘,‘r‘)tar.extractall()  # 可设置解压地址tar.close()

  

Python3 's Shutil module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.