Read/write
You can use modes such as W,A,R,WB,AB,RB
With open (' xxx.xx ', ' WB ') as F:f.write (' xxxxxxxxx ')
Copy
If destination is a folder, the source file is copied to destination and the original file name is maintained
If destination is the end of the filename, it will be the new name of the copied file
Shutil.copy (source, destination)
Move
If destination is a folder, the source file is moved to destination and the original file name is maintained
If the source file already exists in the destination folder, the file with the same name in the destination folder will be overwritten
If there is no destination folder, the source will be renamed to destination
Shutil.move (source, destination)
Delete
Os.unlink (file) Delete file files (individual files)
Os.rmdir (path) deletes the path folder, which must be empty, without any files and subfolders (single empty folder)
Shutil.rmtree (path) deletes the path folder, all files and subfolders it contains are deleted
Send2trash.send2trash (file) sends folders and files to the computer's trash or Recycle Bin, rather than permanently deleting them
The Os.walk () function is passed in a string value, which is the path to a folder. You can have a for
Os.walk () traverses the directory tree, returning 3 values:
1. The name of the folder that is currently traversed;
2. A list of subfolders in the current folder;
3. The list of files in the current folder.
Compression
# New compressed file is opened in write mode, and write mode erases all the original contents in the zip file
# The second argument passed in ' a ' is opened in Add mode, the file is added to the original ZIP file,
>>> newzip = ZipFile. ZipFile (' New.zip ', ' W ')
The # Write () method passes the first parameter to the file, compresses the file, adds it to the zip file, and the second parameter is "compression type", which tells the computer what algorithm to use to compress the file
>>> newzip.write (' Spam.txt ', compress_type=zipfile. zip_deflated)
>>> Newzip.close ()
Open compressed file
>>> examplezip = ZipFile. ZipFile (' Example.zip ')
# View a list of all files and folders in a compressed file
>>> examplezip.namelist ()
[' Spam.txt ', ' cats/', ' cats/catnames.txt ', ' cats/zophie.jpg ']
# returns the Zipinfo object for the specified file, Zipinfo object has its own properties
>>> spaminfo = examplezip.getinfo (' spam.txt ')
# Original File size
>>> spaminfo.file_size
13908
# File size after compression
>>> spaminfo.compress_size
3828
>>> ' compressed file is%sx smaller! '% (round (spaminfo.file_size/spaminfo.compress_size, 2))
' Compressed file is 3.63x smaller! '
>>> Examplezip.close ()
Extract
# Extractall () unzip all files and folders from the zip file and put them in the current working directory
>>> examplezip = ZipFile. ZipFile (' Example.zip ')
>>> Examplezip.extractall ()
# Pass the folder name to Extractall () and the file will be extracted to the specified directory
>>> examplezip.extractall (' c:\\ delicious ')
>>> Examplezip.close ()
# extract () extract a single file from the zip file, the second parameter is extracted to the specified directory, if the specified folder does not exist, it is automatically created
>>> examplezip.extract (' Spam.txt ')
' C:\\spam.txt '
>>> examplezip.extract (' spam.txt ', ' c:\\some\\new\\folders ')
' C:\\some\\new\\folders\\spam.txt '
>>> Examplezip.close ()
Python action file