This article is participating in Python Theme Month. See the link for details

About shutil

It is a high – level file -, folder -, and package – handling module built into Python.

File operation correlation

Use 1

shutil.copyfileobj(fsrc, fdst[, length])
Copy the code

Copy the contents of a file to another file

import shutil

shutil.copyfileobj(open('old.xml','r'), open('new.xml', 'w'))
Copy the code

Use 2

shutil.copyfile(src, dst)
Copy the code

Copy files

Shutil.copyfile ('f1.log', 'f2.log') # The target file does not need to existCopy the code

Use 3

shutil.copymode(src, dst)
Copy the code

Copy permission only. The contents, groups, and users remain unchanged

Shutil.copymode ('f1.log', 'f2.log') # The target file must existCopy the code

Use 4

shutil.copystat(src, dst)
Copy the code

Only the mode bits, ATIme, Mtime, and Flags information is copied

Shutil.copystat ('f1.log', 'f2.log') # The destination file must existCopy the code

Use 5

shutil.copy(src, dst)
Copy the code

Copy files and permissions

import shutil
 
shutil.copy('f1.log', 'f2.log')
Copy the code

Use 6

shutil.copy2(src, dst)
Copy the code

Copy files and status information

import shutil

shutil.copy2('f1.log', 'f2.log')

Copy the code

Use 7

shutil.ignore_patterns(*patterns)
shutil.copytree(src, dst, symlinks=False, ignore=None)
Copy the code

Recursively copy folders

Import shutil shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'TMP *')) Note that the parent directory of folder2 must have writable permissions. Ignore means excludeCopy the code

Usage of eight

shutil.rmtree(path[, ignore_errors[, onerror]])
Copy the code

Delete files recursively

import shutil
 
shutil.rmtree('folder1')
Copy the code

Usage of eight

shutil.move(src, dst)
Copy the code

Move the file recursively, like the mv command, but rename it.

import shutil
  
shutil.move('folder1', 'folder3')
Copy the code

Compressed package correlation

Basic usage

shutil.make_archive(base_name, format,...)
Copy the code

Create a zip package and return the file path, such as zip or tar

Base_name: indicates the file name or path of the compressed package. If the file name is a file name, the file is saved to the current directory; otherwise, the file is saved to the specified path. For example, base_name='data_bak' => Save to the current path. For example, base_name='/ TMP /data_bak' => Save to/TMP/format: Compressed package type: zip, tar, bztar, gztar root_dir: path of the folder to be compressed (default current directory) owner: user, default current user group: group, default current group Logger: Used for logging, usually a logging.logger objectCopy the code

Scenario 1

Package the files under /data and place them in the current program directory

import shutil
ret = shutil.make_archive("data_bak", 'gztar', root_dir='/data')
Copy the code

Scenario 2

Package the files under /data and place them in/TMP /

import shutil
ret = shutil.make_archive("/tmp/data_bak", 'gztar', root_dir='/data')
Copy the code

There’s more to discover!