python built-in module (shutil) --034

1. Introduction

shutil is an advanced file, folder, and compressed package processing module

 

2. Use

1. Copy the contents of the file to another file [shutil.copyfileobj(fsrc, fdst[, length])]

import shutil

f1 = open("E:\python-project\\f1.txt",'r',encoding='utf-8')

f2 = open("E:\python-project\\f2.txt",'w',encoding='utf-8')

shutil.copyfileobj(f1,f2)

  

 

2. Copy files (copy files) [shutil.copyfile(src, dst)]

import shutil

shutil.copyfile('E:\python-project\\f1.txt','E:\python-project\\f3.txt')

  

 

3. Only copy permissions, content, group, and user are unchanged [shutil.copymode(src, dst)]

import shutil

shutil.copymode("E:\python-project\\f1.txt",'E:\python-project\\f3.txt')

  

 

4. Only copy status information, including: mode bits, atime, mtime, flags [shutil.copystat(src, dst)]

import shutil

shutil.copystat("E:\python-project\\f1.txt",'E:\python-project\\f3.txt')

 

 

5. Copy files and permissions [shutil.copy(src, dst)]

import shutil

shutil.copy("E:\python-project\\f1.txt",'E:\python-project\\f4.txt')

 

  

6. Copy files and status information [shutil.copy2(src, dst)]

import shutil

shutil.copy2("E:\python-project\\f1.txt",'E:\python-project\\f5.txt')

  

 

7. Recursively copy the folder [shutil.copytree(src, dst, symlinks=False, ignore=None)]

import shutil

shutil.copytree("E:\python-project\Atm",'E:\python-project\\a')

  

 

8. Delete files recursively [shutil.rmtree(path[, ignore_errors[, onerror]])]

import shutil

shutil.rmtree('E:\python-project\\a')

  

 

9. Recursively move files, similar to the mv command, but actually rename [shutil.move(src, dst)]

import shutil

shutil.move('E:\python-project\\f5.txt','E:\python-project\\f6.txt')

  

 

10. Processing of compressed packages [shutil.make_archive(base_name, format,...)]

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

base_name: The file name of the compressed package, or the path of the compressed package. If it is only the file name, it will be saved to the current directory, otherwise it will be saved to the specified path.

Such as: www => save to the current path

Such as: /Users/wupeiqi/www => save to /Users/wupeiqi/

format: the type of compressed package, "zip", "tar", "bztar", "gztar"

root_dir: Folder path to be compressed (default current directory)

owner: user, default current user

group: group, default current group

logger: used to record logs, usually a logging.Logger object

 

Example: Package the Atm directory under E:\python-project as ff.zip and put it in the E:\python-project directory

import shutil

shutil.make_archive ('E: \ python-project \\ ff', 'zip', 'E: \ python-project \ Atm')

  

 

11. The processing of compressed packages by shutil is carried out by calling two modules: ZipFile and TarFile

(1) zipfile compression and decompression (compresses the specified file)

Pack:

import zipfile

z= zipfile.ZipFile('E:\python-project\\a.zip','w')

z.write('E:\python-project\\f6.txt')

z.write('E:\python-project\\f1.txt')

z.close()

  

Unzip:

z = zipfile.ZipFile('E:\python-project\\a.zip','r')

z.extractall()

z.close()

  

 

(2) tarfile compression and decompression

Pack:

import tarfile

tar = tarfile.open('E:\python-project\\b.zip','w')

tar.add('E:\python-project\\f4.txt')

tar.add('E:\python-project\\f2.txt')

tar.close()

  

Unzip:

tar = tarfile.open('E:\python-project\\b.zip','r')

tar.extractall()

tar.close()

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325911116&siteId=291194637