This is the third day of my participation in the August More text Challenge. For details, see:August is more challenging

โœฐ Review and review

We’ve already learned the OS module that operates on files, and the os.path module that operates on file paths.

In this installment, we continue our study of files in Python’s shutil module.

(#^ ^.^#), learn the shutil module to compress, move, copy files and folders and other advanced operations

โœง1. overview of the shutil module

Shutil module shutil module shutil module shutil module shutil module shutil module shutil module shutil module

  • Python standard library provides, mainly used to do files and folders to copy, move, delete, etc
  • Supports compression and decompression of files and folders
  • The Shutil module complements the usual operations on directories or files provided by the OS module, providing operations such as moving, copying, compression, decompression, etc. It is an advanced file operation module

๐Ÿ’ป note

The copy files provided by the shutil module are also metadata that cannot be copied to files. (Metadata: information such as file size and modification date.)

  • On Linux systems, owners and groups, as well as security data, are lost after the source file is copied
  • On the MAC OS, file operation records and other metadata cannot be used. As a result, the information about the creator of the copied file is lost. As a result, the information displayed is inaccurate
  • On Windows, copied files do not copy file owners, ACLs, and alternate data streams

โœง2. Directory file operations

The shutil module provides the ability to copy and move files that the OS module does not. Common methods are as follows:

methods role
shutil.copyfileobj(fsrc,fdst[length=16*1024]) To copy the contents of a file to another file, you can specify the size of the contents
shutil.copyfile(src,dst) Copy files
shutil.copymode(src,dst) Only copy permission. Contents, groups, and users are unchanged
shutil.copystat(src,dst) Copy only the status information,
The value can be Mode bits, atime, mtime, and flags
shutil.copy(src,dst) Copy files and permissions
shutil.copy2(src,dst) Copy files and status information
shutil.ignore_patterns(*patterns) Recursively copy file contents and status information. Ignore means exclude
shutil.copytree(src,dst,symlinks=False,ignore=None,ignore=None) Copy soft link
shutil.rmtree(path[,ignore_errors[,onerror]]) Delete files recursively
shutil.move(src,dst) To move the file recursively, it’s like the mv command, but it’s just renaming.

๐ŸŒŸ shutil. Used by copyfile (SRC, DST)

Copy the file ๐Ÿ“ƒ; You can specify the size of the file.

Note: both SRC and DST must be files, not directories. Otherwise IOError: [Errno 13] Permission denied:

Let’s implement, from A folder under the file batch copy B folder

import shutil import os def copyfile(): Print (" file: ",filefolds) I = 0 for file in filefolds: i = i+1 new_name = "New/"+"New"+str(i)+"_test"+'.txt' print(new_name) filename = "old/"+file+"/test.txt" shutil.copyfile(filename, new_name) copyfile()Copy the code

๐ŸŒŸ shutil. Copytree (SRC, DST, symlinks = False, ignore = None, ignore = None)

Recursively copy files and status information for directories and subdirectories

  • SRC: source directory (address of the content to be copied)
  • DST:If it exists, it is not overridden;Otherwise it will be reportedWindowsError: [Error 183]
  • Symlinks: Specifies whether to copy the soft links. Beware of getting stuck in an endless loop
  • Ignore: Specifies the file that will not be copied. The value should be an ignore_patterns() method
  • Cpoy_founction: specifies the replication mode
Import OS import shutil # switch to os.chdir(r"F:\JueJin") shutil.copytree("./old/old0_test","./New3") Print (" DST directory: ",os.listdir("./New3"))Copy the code

โœง3. Decompress the file

The shutil module also provides the ability to package, compress, and unpack files ๐Ÿ“ƒ and ๐Ÿ“. The common methods are as follows:

methods role
shutil.make_archive(base_name,format[,root_dir[,base_dir[]]]) Create an archive file (such as zip or tar) and return its name
shutil.get_archive_formats() Returns a list of supported archive formats
shutil.register_archive_format(name,funvtion[,extra_args[,description]]) Register an archive for the name format

Ps: Shutil module supports compression and decompression operations. It relies on zipFile module and Tarfile module

๐ŸŒŸ shutil. Make_archive (base_name, format, root_dir [[, base_dir […]]])

“Zip “,” tar”,”bztar”or”gztar” root_dir: which directory or file to package (i.e., the source file)

Copy the code

โœ summary

In this issue, we will learn the shutil module to complement the OS module for file operations, with two key points:

  • Files can be copied, moved, and deleted
  • Can compress and decompress files and folders

If you need to copy, move, compress, and decompress files, you can use the Shutil module in Python to improve the speed of solving problems

The above is the content of this issue, welcome the big guys to like comment correction, see next time ~แƒฆ(ยด ยท แด— ยท ‘) heart