Explanation of python shutil Operation File Example

  • 2021-10-15 10:56:37
  • OfStack

1. Create an archive file using the function shutil.make_archive () and return the archive name.

import shutil
path_1 = r'D:\PycharmProjects\Hello'
path_2 = r'D:\PycharmProjects\Hello\shutil-test'
new_path = shutil.make_archive(path_2,'zip',path_1)

2. Parse the split file using the function shutil. unpack_archive (filename [, extract_dir [, format]]).

filename is the complete path for archiving extract_dir is the target directory name of the unzipped archive format is the format of the extracted file

import shutil
import os

Extension of knowledge points:

Use of shutil module of Python

1. Fast file copying

2. Fast file compression

3. Fast recursive replication of file directories


#Author Kang

import shutil

# Think of it as a directory nginx.conf Files are copied to another 1 Directories, and rename them 

# Put base_dir Directory of 20190218 Packaged and compressed to the desktop test.zip
shutil._make_zipfile("/Users/kang/Desktop/test",base_dir='/Users/kang/PycharmProjects/ Do not break or stand / No. 1 2 Chapter /20190218',)

# Put 20190218 Copy all the contents under to the desktop kangtest Directory ( kangtest Does not exist, the program will create a new 1 Directories) 
shutil.copytree('/Users/kang/PycharmProjects/ Do not break or stand / No. 1 2 Chapter /20190218/','/Users/kang/Desktop/kangtest')

Related articles: