Python Basics-4.6 shutil module

Source: Internet
Author: User

Shutil Module

Advanced files, folders, Compression pack processing modules

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

import shutilshutil.copyfileobj(open(‘old.xml‘,‘r‘), open(‘new.xml‘, ‘w‘))

Shutil.copyfile (SRC, DST)
Copy files

shutil.copyfile(‘f1.log‘, ‘f2.log‘) #目标文件无需存在

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

shutil.copymode(‘f1.log‘, ‘f2.log‘) #目标文件必须存在

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

shutil.copystat(‘f1.log‘, ‘f2.log‘) #目标文件必须存在

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

import shutilshutil.copy(‘f1.log‘, ‘f2.log‘)

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

import shutilshutil.copy2(‘f1.log‘, ‘f2.log‘)

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

import shutilshutil.copytree(‘folder1‘, ‘folder2‘, ignore=shutil.ignore_patterns(‘*.pyc‘, ‘tmp*‘)) #目标目录不能存在,注意对folder2目录父级目录要有可写权限,ignore的意思是排除

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

import shutilshutil.rmtree(‘folder1‘)

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

import shutilshutil.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,

such as Data_bak = Save to Current path
such as:/tmp/data_bak = Save to/tmp/

    • 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
#将 /data 下的文件打包放置当前程序目录import shutilret = shutil.make_archive("data_bak", ‘gztar‘, root_dir=‘/data‘)#将 /data下的文件打包放置 /tmp/目录import shutilret = shutil.make_archive("/tmp/data_bak", ‘gztar‘, root_dir=‘/data‘)

Shutil Processing of compressed packets is done by calling the ZipFile and Tarfile two modules, in detail:
ZipFile Compression & Decompression

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(path=‘.‘)z.close()

Tarfile Compression & Decompression

import tarfile# 压缩>>> t=tarfile.open(‘/tmp/egon.tar‘,‘w‘)>>> t.add(‘/test1/a.py‘,arcname=‘a.bak‘)>>> t.add(‘/test1/b.py‘,arcname=‘b.bak‘)>>> t.close()# 解压>>> t=tarfile.open(‘/tmp/egon.tar‘,‘r‘)>>> t.extractall(‘/egon‘)>>> t.close()

Python Basics-4.6 shutil module

Related Article

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.