Advanced files, folders, Compression pack processing modules
Shutil.copyfileobj (FSRC, fdst[, length])
Copy the contents of a file to another file
Mport shutilshutil.copyfileobj (Open (' old '), open (' New ', ' W '))
Shutil.copyfile (SRC, DST)
Copy files
Shutil.copyfile (' F1.log ', ' F2.log ') # Copy files, destination files need not exist
Shutil.copymode (SRC, DST)
Copy permissions only. Content, group, user are not changed
Shutil.copymode (' F1.log ', ' F2.log ') # The destination file must exist
Shutil.copystat (SRC, DST)
Copy only the status information, including: mode bits, Atime, mtime, Flags
Shutil.copystat (' F1.log ', ' F2.log ') # The destination file must exist
Shutil.copy (SRC, DST)
Copying files and Permissions
Shutil.copy (' F1.log ', ' F2.log ')
Shutil.copy2 (SRC, DST)
Copying files and status information
Shutil.copy2 (' F1.log ', ' F2.log ')
Shutil.ignore_patterns (*patterns)
Shutil.copytree (SRC, DST, Symlinks=false, Ignore=none)
Recursive go-to-copy folder
Shutil.copytree (' Folder1 ', ' Folder2 ', ignore=shutil.ignore_patterns (' *.pyc ', ' tmp* ')) #目标目录不能存在, Note for Folder2 directory parent directory to have writable permissions, ignore means to exclude
Shutil.rmtree (path[, ignore_errors[, OnError])
To delete a file recursively
Shutil.rmtree (' Folder1 ') # recursively go to delete files directory
Shutil.move (SRC, DST)
To move a file recursively, it is like the MV command, which is actually renamed.
Shutil.move (' Folder1 ', ' Folder2 ') # Move folder, like MV
Shutil.make_archive (base_name, Format,...)
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
ret = shutil.make_archive (' My_proj_bak ', ' Gztar ', root_dir= ' my_proj ')
Shutil Processing of compressed packets is done by calling the ZipFile and Tarfile two modules, in detail:
ZipFile Compression & Decompression
Import Zipfilez = ZipFile. ZipFile (' Log.zip ', ' W ') z.write (' F1.log ') z.write (' F2.log ') z.close ()
z = zipfile. ZipFile (' Log.zip ', ' R ') Z.extractall (path= '. ') Z.close ()
Tarfile Compression & Decompression
Import Tarfilet = Tarfile.open (' Log.tar ', ' W ') t.add (' F1.log ', arcname= ' F1.bak ') t.close ()
Getting started with Python: Common modules-shutil Modules