Python Advanced — File Processing (IO)
A complete program generally includes data storage and reading; In the actual development, we often need to read data from external storage media (hard disk, CD, U disk, etc.), or store the data generated by the program in files to achieve “persistent” storage.
Text files and binary files
- Text files Text files store plain “character” text that defaults to the Unicode character set and can be opened using Notepad.
- Binary files Binary files store data contents in bytes and cannot be opened with Notepad. The corresponding software must be used to decode; Such as MP4, Mp3, JPG, DOC and so on.
File operation related modules
The name of the | instructions |
---|---|
IO module | File stream input and output operations |
OS module | Into the operating system functions, including file operation |
The glob module | Finds a specific file pathname that matches the rule |
The fnmatch module | Use modules to match file pathnames |
The fileinput module | Process multiple input files |
Filecmp module | Used for file comparison |
CVS module | Used for CVS file processing |
Pickle and cPickle | Used for serialization and deserialization |
XML package | For XML data processing |
Bz2, gzip, zipfile, zlib, tarfile | For handling compressed and decompressed files (different algorithms) |
Create file object open()
The open() function is used to create file objects.
Open (filename [, opening method])Copy the code
If only the file name, it represents the file in the current directory. The file name can be entered in the full path, for example, D:\a\b. TXT.
Opening mode:
model | describe |
---|---|
r | Read mode |
w | Write mode, create if the file does not exist; Existing to rewrite the new content |
a | Append mode. If the file does not exist, create it and append to the end of the file |
b | Binary mode (can be used in combination with other modes) |
+ | Read and write modes (same as above) |
Basic file writing and other operations
Three steps:
- Creating a file Object
- Write data
- Close the file object
[Operation] Writes data
# absolute path
# file = open(
# "D:/00.study/ study-python / 02.python 更 新 / day02-python 更 新 /code/file.txt",
# "a",
# encoding="utf-8")
# a = "wuhu!!
# file.write(a)
# file.close()
# relative path (need to change vscode configuration)
file = open("file.txt"."a", encoding="utf-8")
a = "Wuhu!!
file.write(a)
file.close()
Copy the code
Introduction to common Codes
Garbled characters are encountered when operating Chinese. Coding relation:
Chinese garbled characters
The default code for Windows is GBK, and the default code for Linux is UTF-8. When we call open(), we call the operating system open folder, which is not encoded GBK by default.
Write ()/writeLines() Write data
Write (a) : Write string A to a file. Writeline (b) : Write a list of strings to a file without adding newlines.
[Operation] test
f = open(r"file.txt"."w", encoding="utf-8")
s = ["sue\n"."Jason\n"."Eason\n"."Forrest\n"]
f.writelines(s)
f.close()
Copy the code
Results: the file. TXT
sue
Jason
Eason
Forrest
Copy the code
Close () Closes the file stream
The bottom layer of the file is controlled by the operating system, so opening the file object must first display the close() method to close the file object. When the close() method is called, the buffer data is written to the file (you can also call flush() directly), and the file is closed and the file object is released.
To ensure that open file objects close properly, it is common to implement finally or with the exception mechanism to close open file objects in any case.
[Action] Use exceptions and finally to ensure file closure
try:
file = open(r"file.txt"."a", encoding="utf-8")
str = "Sue, handsome ass!! \n"
file.write(str)
except BaseException as e:
print(e)
finally:
file.close()
Copy the code
The with statement (context management) ⭐
The with keyword (context manager) automatically manages context resources, ensures that files close properly for whatever reason you leave the with module, and automatically restores the scene when the code block is executed.
[Operation] test
arr = ["primary\n"."middle\n"."high\n"]
with open(r"file.txt"."a", encoding="utf-8") as file:
file.writelines(arr)
Copy the code
With keeps both the open and closed operations on one side without having to close them ourselves
Text file reading
There are three ways to read a file:
- Read ([size]) Reads size characters in a file and returns them as the result. If there is no size parameter, the entire file is read. Read to the end of the file, returning an empty string.
- Readline () reads one line of content and returns the result. Read to the end of the file, returning an empty string.
- Readlines () text file, where each line is stored as a string in a list that is returned.
【 operation 】read() method
with open(r"file.txt"."r", encoding="utf-8") as f:
print(f.read(10)) # Sue, Jason,
Copy the code
Readline () method
with open(r"file.txt"."r", encoding="utf-8") as f:
print(f.readline()) # Sue, Jason, Eason, Forrest, primary, middle, high
Copy the code
Readlines (
with open(r"file.txt"."r", encoding="utf-8") as f:
print(f.readlines())
# [' Sue, Jason, \n', 'Eason, Forrest, \n', 'primary, middle, \n', 'high\n']
Copy the code
3. Operation Practice:
with open(r"file.txt"."r", encoding="utf-8") as file:
lines = file.readlines()
print(lines)
lines = [
line.strip() + "-" + str(index + 1) + "\n"
for index, line in enumerate(lines)
]
print(lines)
with open(r"file.txt"."w", encoding="utf-8") as file:
file.writelines(lines)
Copy the code
Reading and writing of binary files
The processing flow of binary files is the same as that of text files. You still need to create the file object first, but you need to specify binary mode to create the binary file object.
Example:
file = open(r"xxx.txt"."wb") Binary file object in writable, rewritable mode
file = open(r"xxx.txt"."ab") Binary file objects in writable, append mode
file = open(r"xxx.txt"."rb") # readable binary file object
Copy the code
After the file is created, you can use write() and read() to read and write the file
[Operation] Copy a binary file
with open(r"cat.gif"."rb") as f: The binary file to copy
with open(r"copy_one.gif"."wb") as w: # Copied binary file
for line in f.readlines():
w.write(line)
print("Copy over")
Copy the code
Common properties and methods for file objects
File objects encapsulate file-related operations. The following illustrates common attributes and methods
Properties:
attribute | instructions |
---|---|
name | Returns the name of the file |
mode | Returns the open mode of the file |
closed | Returns True if the file is closed |
Open mode of file object:
model | instructions |
---|---|
r | If the file does not exist, an exception is thrown |
w | In write mode, the existing files are cleared |
x | Write mode, creates a new file, throws an exception if the file exists |
a | Append mode, does not overwrite the original content of the file |
b | Binary mode (can be combined with other modes) |
t | Text mode (default, omitted) |
+ | Read/write mode (can be combined with other modes) |
Common methods for file objects:
The file name | instructions |
---|---|
read([size]) | Returns size bytes or characters read from a file. If size is omitted, read all |
readline() | Reads a line from a text file |
readlines() | Each line read from the text file is returned as a separate string object, and these objects are returned in a list |
write(str) | Writes the string STR content to the file |
writelines(s) | The stringThe list ofS writes to the file without adding a newline character |
seek(offset[,whence]) | Move the file pointer to the new location, offset represents the location for whence: offset: Off means moving towards the end and negative means moving towards the beginning whence means different things: 0: evaluates from the file header (default) 1: Starts from the current position 2: starts from the end of the file |
tell() | Returns the current position of the file pointer |
flush() | Writes the contents of the buffer to a file, but does not close the file |
close() | Writes the contents of the buffer to a file, closes the file, and frees the file object |
truncate([size]) | Deletes the current pointer position to the end of the file. If size is specified, only the first size bytes are left regardless of the position of the pointer, and the rest is deleted |
writeable() | Tests whether the current file is writable |
readable() | Tests whether the current file is readable |
[Operation] Test seek method and name, mode, closed attribute
with open(r"file.txt"."r", encoding="utf-8") as f:
f.seek(2) # index -- pointer
print(f.read())
# e - 1
# Jason,---2
# Eason,---3
# Forrest,---4
- 5 # primary
# middle - 6
# high - 7
print(f.name) # file.txt
print(f.mode) # r
print(f.closed) # False
print(f.closed) # True
Copy the code
[Operation] Test the TRUNCate and Flush methods
# -------------flush------------------
file = open(r"file.txt"."r", encoding="utf-8")
file.flush() # write also does not write directly, but also puts data into a buffer
file.close()
# ----------truncate--------------
f = open("file.txt"."a", encoding="utf-8")
f.truncate(20)
f.close()
f = open("file.txt"."r", encoding="utf-8")
print(f.read())
# sue,---1
# Jason.
Copy the code
Pickled serialization
In Python, an object is essentially a “block of memory that stores data.” When we need to save the data in the memory block to the hard disk, or transfer to another computer over the network. We need “object serialization and deserialization”. Serialization is used in distributed, parallel systems.
Conversion between in-memory objects and data formats (STR, bites) that are easy to persist and interact on disk or over the network can be implemented. This mechanism is called serialization and serialization
Serialization: The process of converting non-persistent and transportable objects in memory into objects that can be easily persisted and transportable.
Deserialization: The process of converting persistent and transportable objects to non-persistent and transportable objects.
Dumps (), loads(), loads(), loads(), loads(), loads(), loads(), loads(), loads(), loads(), loads(), loads())
Syntax format:
pickle.dump(obj,file) # obj is the object to be serialized, and file is the storage file
pickle.load(file) Read data from file and deserialize it into objects.
Copy the code
[Operation] Serialize an object to a file
import pickle
with open(r"file.txt"."wb") as f:
name = "Dantes"
age = 18
arr = [1.2.3.4.5.6.7]
pickle.dump(name, f)
pickle.dump(age, f)
pickle.dump(arr, f)
Copy the code
[Operation] Deserialize the obtained data
with open(r"file.txt"."rb") as f:
a1 = pickle.load(f)
a2 = pickle.load(f)
a3 = pickle.load(f)
print(a1) # Dantes
print(a2) # 18
print(a3) # [1, 2, 3, 4, 5, 6, 7]
Copy the code
CSV file operation
CSV (Cooma Separated Values) is a comma-separated text format commonly used for data exchange, import and export of Excel files and database data. Unlike Excel files, in CSV files:
- Values have no type; all values are strings
- Font color and style cannot be specified
- Cannot specify the width and height of cells, cannot merge cells
- There are no multiple worksheets
- Cannot embed graphic diagrams
The table established in Excel is:
Save as “CSV” format, the content after opening is as follows:
Read and write CSV files
CSV
File reading
Remember to import the CSV package before using it
import csv
with open(r"xxx.csv"."r") as f:
after_read = csv.reader(f)
# print(after_read) # <_csv.reader object at 0x000001B7C5F1DAC8>
print(list(after_read))
Copy the code
The csv.reader() function creates an object that reads the data
CSV
File writing
a1 = [5."jj".21."doctor"]
a2 = [6."gg".25."runner"]
a3 = [7."kk".31."player"]
with open(r"xxx.csv"."w") as f:
write_csv = csv.writer(f)
write_csv.writerow(a1)
write_csv.writerow(a2)
write_csv.writerow(a3)
Copy the code
OS module (⭐)
The OS module allows us to control the operating system directly. Directly call the operating system executable files, commands, operation files, directories, and so on. It is the core foundation of system operation and maintenance
The OS invokes operating system files and commands
os.system
The system function converts strings into commands to run on the server. The principle is that when each system function is executed, a sub-process is created to execute the command line on the system. The execution result of the sub-process cannot affect the main process.
【 Operation 】 Open notepad
import os
os.system("notepad.exe")
Copy the code
[Operation] Invoke the ping command of the system
os.system("ping www.google.com")
# Pinging www.google.com [157.240.12.35] with 32 bytes of data:
# Request timed out. ...
Copy the code
Open CMD and Powershell
os.system("cmd")
os.system("powershell")
Copy the code
os.startfile
You can open the file based on its root directory
os.startfile(r"D:/WeChat/WeChat.exe")
Copy the code
OS module – File and directory operations
If you need to do other things with files and directories, you can use the OS and os.path modules
Common file operations of OS module:
The method name | describe |
---|---|
remove(path) | Deletes the specified file |
rename(src,dest) | Renames a file or directory |
stat(path) | Returns all attributes of the file |
listdir(path) | Returns a list of files and directories under the path directory |
Use:
os.rename("./cat.gif"."rename_cat.gif")
Copy the code
os.remove("./file1.txt")
Copy the code
file_pro = os.stat("file.txt")
print(file_pro)
# os.stat_result(st_mode=33206, st_ino=3659174697668209, st_dev=1986293374, st_nlink=1, st_uid=0, st_gid=0, st_size=43, st_atime=1633682811, st_mtime=1633607574, st_ctime=1633508935)
Copy the code
file_list = os.listdir("D:/00.study/")
print(file_list)
# ['Basic-Study', 'Full-Stack-Study', 'hooks-app', 'Study-C And C++', 'Study-Deep-JavaScript', 'Study-JavaScript- Dredrel ', 'Study-Python', 'Vie-manage-system-master ',' Work ', 'Review document ']
print(os.name) # nt --> windows; linux&unix -->posix
Copy the code
Related methods of directory operation in OS module:
The method name | describe |
---|---|
mkdir(path) | Create a directory |
makedirs(path1/path2/…) | Creating a Multi-level directory |
rmdir(path) | Delete the directory |
removedirs(path1/path2/…) | Deleting a Multi-level Directory |
getcwd() | Return the current working directory: current work dir |
chdir(path) | Set path to the current working directory |
walk() | Traverse the directory tree |
sep | The path separator used by the current operating system |
Use:
os.mkdir("works")
Copy the code
os.makedirs("works/jobs/stu")
Copy the code
os.rmdir("works")
Copy the code
os.removedirs("works/jobs/stu")
Copy the code
os.getcwd()
os.chdir("xxx")
os.walk()
Copy the code
Os. path module – Directory operation
The os.path module provides directory-related operations (path determination, path segmentation, path linking, folder traversal)
methods | describe |
---|---|
isabs(path) | Check whether path is an absolute path |
isdir(path) | Check whether path is a directory |
sifile(path) | Check whether path is a file |
exists(path) | Checks whether the file at the specified path exists |
getsize(filename) | Return file size |
abspath(path) | Return to absolute path |
dirname(path) | Returns the path to the directory |
getatime(filename) | Returns the last access time of the file |
getmtime(filename) | Returns the last modification time of the file |
walk(top,func,arg) | Recursive methods traverse the directory |
join(path,*paths) | Join multiple paths |
split(path) | Splits the path and returns it as a list |
splitext(path) | The extension to split a file from a path |
print(os.path.isabs("file.txt")) # False
print(os.path.isdir("file.txt")) # False
print(os.path.isfile("file.txt")) # True
print(os.path.exists("file.txt")) # True
print(os.path.getsize("file.txt")) # 43
print(os.path.abspath("file.txt"))
# D:\00.study\ study-python \02.Python pro \day02 -Python pro \code\file.txt
print(os.path.dirname(__file__))
# d:\00.study\ study -Python\02.Python pro \day02 -Python pro \code
print(os.path.getatime("file.txt")) # 1633682811.951791
print(os.path.getmtime("file.txt")) # 1633607574.351463
# print(os.path.join())
print(os.path.split("file.txt")) # ('', 'file.txt')
print(os.path.splitext("file.txt")) # ('file', '.txt')
Copy the code
Walk () recursively traverses all files
OS. Walk () method:
Returns a tuple of 3 elements. (dirpath, dirnames, filenames)
- Dirpath: lists the path of the specified directory
- Dirnames: all favorite folders of a directory
- Filenames: all files in the directory
Print the file name in the directory by walking up or down the directory tree. A simple and easy to use file, directory traversal, can help us efficiently deal with the file, directory things.
The walk() method syntax is as follows:
os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
Copy the code
Parameters:
- top— is the address of the directory you want to traverse, and returns a triplet (root,dirs,files).
- Root refers to the address of the folder itself that is being traversed
- Dirs is a list of the names of all directories in that folder (excluding subdirectories)
- Files is also a list of all files in this folder (excluding subdirectories).
- Topdown — Optional. If this parameter is True, top directories are preferentially traversed; otherwise, subdirectories of top are preferentially traversed. (This parameter is enabled by default.) If the topDown parameter is True, walk traverses the top folder and each subdirectory in the top folder.
- Onerror — Optional, requires a Callable object that will be called when walk needs an exception.
- Followlinks — Optional, if True, traverses the directory that the shortcut in the directory (Linux, soft link symbolic link) actually refers to (off by default), if False, traverses the subdirectories of top first.
Use:
# os.makedirs("works/some/www")
path = os.getcwd()
# print(path)
file_lists = os.walk(path)
# print(list(file_lists))
for dirpaths, dirnames, filenames in file_lists:
# print(dirpaths)
# print(dirnames)
# print(filenames)
for dirP in dirnames:
# print(dirP)
print(os.path.join(dirpaths, dirP))
for names in filenames:
print(os.path.join(dirpaths, names))
Copy the code
Shutil module (copy and compress)
The Shutil module is provided in the Python standard library and is used for copying, moving, and deleting files and folders. You can also compress and decompress files and folders.
The OS module provides general operations on directories or files, supplemented by the Shutil module, which provides operations such as moving, copying, compressing, and decompressing
methods | describe |
---|---|
copyfileobj(src, dst) | TXT file and write to new.txt file |
shutil.copyfile(src, dct) | Copy from the source SRC to DST. Of course, the premise is that the destination address is writable. The exception message thrown is IOException. It will be overwritten if the current DST already exists |
shutil.copymode(src, dst) | It just copies its permissions and nothing else gets copied |
shutil.copystat(src, dst) | Replication permission, last access time, and last modification time |
shutil.copy( src, dst) | Copy a file to a file or directory |
shutil.copy2( src, dst) | The last access time and modification time are also copied over, similar to cp -p |
shutil.copy2( src, dst) | If two file systems are the same, rename is the same operation. Move if it’s not on the same file system |
shutil.copytree( olddir, newdir, True/Flase) | Copy olddir to newdir. If the third argument is True, the directory will be copied with symbolic links in the folder. If the third argument is False, a physical copy will be generated in place of symbolic links in the copied directory |
shutil.rmtree( src ) | Recursively deletes a directory and all its contents |
shutil.ignore_patterns(*patterns) | Which file to ignore, selective copy |
shutil.make_archive(base_name, format[, root_dir[, base_dir[, verbose[, dry_run[, owner[, group[, logger]]]]]]]) | Create a zip package and return the file path, such as zip or tar |
Use:
Shutil. Copyfileobj (SRC, DST) :
shutil.copyfileobj(open("old.txt"."r"), open("new.txt"."w"))
os.system("cat old.txt new.txt")
Copy the code
You must open the file with open(” XXXX “,”x”) and write to it, otherwise copyFileobj will only copy the contents of the file
Shutil. Used by copyfile (SRC, DCT) :
shutil.copyfile("old.txt"."new1.txt")
os.system("cat old.txt new.txt")
Copy the code
Copy from the source SRC to DST. Of course, the premise is that the destination address is writable. IOException is thrown. If the current DST already exists, the file will be overwritten. If not, the file will be overwritten
*shutil.copymode(SRC, DST, follow_symlinks=True) :
shutil.copymode("old.txt"."new1.txt")
Copy the code
Only files can be copied. The contents, groups, and users of files remain unchanged
*shutil.copystat(SRC, DST, follow_symlinks=True) :
shutil.copystat("old.txt"."new1.txt")
os.system("stat old.txt new.txt")
Copy the code
Copy file status information, the file must exist, do not change the copy time
Shutil. copy(SRC, DST, *, follow_symlinks=True) : shutil.copy(SRC, DST, *, follow_symlinks=True)
shutil.copy("old.txt"."new2.txt")
os.system("stat old.txt new2.txt")
Copy the code
Copy files and status information without changing the time
Shutil. Copy2.c (SRC, DCT) :
shutil.copy("old.txt"."new2.txt")
os.system("stat old.txt new2.txt")
Copy the code
Copy files and status information
Shutil. copytree(SRC, DST, symlinks=False, ignore=None) :
os.system("tree works")
shutil.copytree(
"works"."works2",
symlinks=True,
)
os.system("tree works2")
Copy the code
Symlinks =True copy only linked files. If False copy source files. Ignore = uncopied files or directories
Shutil. Rmtree (path, ignore_errors = False, onerror = None) :
os.system("ls -d works2")
shutil.rmtree("works2")
os.system("ls -d works2") # ls: works2: No such file or directory
Copy the code
Shutil. Move (SRC, DST, copy_function = copy2.c) :
os.system("ls -ld works")
# drwxr-xr-x 3 SUe Administrators 0 Oct 8 17:59 works
shutil.move("works"."works2")
os.system("ls -ld works")
# ls: works: No such file or directory
os.system("ls -ld works2")
# drwxr-xr-x 3 SUe Administrators 0 Oct 8 17:59 works2
Copy the code
Move the file recursively, like the mv command, but rename it
Shutil. make_archive(base_name, format[, root_dir[, base_dir[, verbose[, dry_run[, owner[, group[, logger]]]]]]]) : shutil.make_archive(base_name, format[, root_dir[, base_dir[, verbose[, dry_run[, owner[, group[, logger]]]]]]]) :
Base_name: indicates the file name or path of the compressed package. If only the file name is a file name, the file is saved to the current directory. Otherwise, the file is saved to the specified directory, for example, tar_name => Save to the current directory. For example, /Users/a6/tar_name => Save to /Users/a6/ format: Compressed package type: zip, tar, bztar, gztar root_dir: path of the folder to be compressed (default current directory) owner: user, default current user group: group, default current group Logger: Used for logging, usually a logging.logger object
ZipFile module (unzip)
Zipfile is used in Python to compress and decompress zip encoding. This module is used frequently because zip encoding is very common.
Common methods:
methods | describe |
---|---|
Is_zipfile (documents) | Test filename’s file to see if it is a valid zipfile |
ZipFile(filename[,mode[,compression[,allowZip64]]]) | – filename: indicates a file object -mode: optional. R, W, and A indicate the openness of different files. R read-only; W to rewrite; To add a -compression: indicates the compression method for this zipfile. The default is ZIP_STORED, and the alternative is ZIP_DEFLATED -allowZip64: is a bool that, when set to True, can be used to create zip files larger than 2 gb. Default: True |
close() | Close the ZIP file and write to it |
extract(member,path=None,pwd=None) | Extract a file from zip |
extractall(path[,pwd]) | Extract the file from the current zip file according to the directory structure in namelist and put it in the specified directory. The path of the extract will be automatically created if it does not exist, and the path must be a directory. The unzip must be a file containing all directories relative to the zip package path. |
namelist() | Returns a list of the paths (relative to the zip file package) of all the subfiles in the zip file. This is a list of zip internal directory structures |
infolist() | Returns a list of ZipInfo objects for subfiles in each ZIP file that have the fields mentioned above |
printdir() | Print the directory structure of the ZIP file to stdout, including the path of each file, change time and size |
open(name[,mode[,pwd]]) | Gets a file object for a subfile that can be used for read,readline,write, and so on |
setpassword(psw) | Set the password for the zip file |
testzip() | Read all files in the ZIP and verify their CRC checksum. Returns the name of the first corrupted file, or None if all files are complete |
write(filename[,arcname[,compression_type]]) | Writes a file named filename outside the zip to a child file named arcname (of course, arcname also has a path to the relative zip package). Compression_type specifies the compression format, which is also ZIP_STORED or ZIP_DEFLATED. The opening mode of Z must be W or A in order to write the file smoothly |
Operation use:
# use of the zipFile module
import zipfile
Is_zipfile checks if the file is a ZIP file
print(zipfile.is_zipfile("works2.zip")) # True
print(zipfile.is_zipfile("works2")) # False
# 2. ZipFile(filename[,mode[,compression[,allowZip64]]])
print(zipfile.ZipFile("works2.zip"."r"))
# <zipfile.ZipFile filename='works2.zip' mode='r'>
# 3.close () to close the file, the end must have
z = zipfile.ZipFile("works2.zip"."r")
z.close()
print(z) # <zipfile.ZipFile [closed]>
# 4. Extract (member,path=None, PWD =None) extract a file from zip
print(z.extract("works2/s.txt", path=None, pwd=None))
# 5. Extractall (path[, PWD]) extracts the file from the current zip according to the directory structure in namelist and puts it in the specified directory
z.extractall("works2")
z.close()
# 6.namelist () returns a list of the paths of all the children in the zip file
print(z.namelist()) # ['works2/', 'works2/some/', 'works2/some/www/']
z.close()
# 7.Infolist () returns a list of ZipInfo objects for subfiles in each ZIP file
print(z.infolist())
# [<ZipInfo filename='works2/' external_attr=0x10>, <ZipInfo filename='works2/some/' external_attr=0x10>, <ZipInfo filename='works2/some/www/' external_attr=0x10>]
z.close()
# 8.printdir () prints the directory structure of the zip file to stdout, including path, change time and size
print(z.printdir())
# File Name Modified Size
# works2/ 2021-10-08 17:59:50 0
# works2/some/ 2021-10-08 17:59:50 0
# works2/some/www/ 2021-10-08 17:59:50 0
z.close()
9. open(name[,mode[, PWD]]), the file object of a subfile, which can be used for read, readline, write, etc# 10.setPassword (PSW), set the zip file password
z.setpassword(123456)
# 11.testzip () reads all files in the zip
a = z.testzip()
print(a is None)
# 12.write (filename[,arcname[,compression_type]]) writes it and the file to zip
z = zipfile.ZipFile("works2.zip"."w")
z.write("files.py")
z.write("os.py")
z.write("shutil.py")
z.close()
Copy the code
Some usage methods:
- Unzip the file and read it
import zipfile
z = zipfile.ZipFile(filename, 'r') The second argument is r to read the zip file, w to create a zip file
for f in z.namelist():
print(f)
Copy the code
- Compress into file to write
import zipfile, os
z = zipfile.ZipFile(filename, 'w') Note that the second argument here is w, where filename is the name of the compressed package
Testdir = testdir = testdir = testdir = testdir
if os.path.isdir(testdir):
for d in os.listdir(testdir):
z.write(testdir+os.sep+d)#os.sep is the file separator "//"
# close() must be called!
z.close()
Copy the code
One-click compression function:
Import the package needed for compression
import zipfile
# Import OS module
import os
# Import the os.path module
import os.path
# 1. Create a class
class ZFile(object) :
# 2. Initialize data filename-filename; Mode - way; Basedir - Absolute path
def __init__(self, filename, mode="r", basedir="") :
self.filename = filename
self.mode = mode
If the write mode is w-write, a-append
if self.mode in ("w"."a") :# Read zip file and set
self.zfile = zipfile.ZipFile(filename,
self.mode,
compression=zipfile.ZIP_DEFLATED)
else:
# If not, default
self.zfile = zipfile.ZipFile(filename, self.mode)
# absolute path
self.basedir = basedir
If not, use os.path.dirname auto-complete
if not self.basedir:
self.basedir = os.path.dirname(filename)
Add a single file
def addfile(self, path, arcname=None) :
# Path removal //, /
path = path.replace("/ /"."/")
#
if not arcname:
if path.startswith(self.basedir):
arcname = path[len(self.basedir):]
else:
arcname = ' '
self.zfile.write(path, arcname)
Whether to add multiple files
def addfiles(self, paths) :
for path in paths:
if isinstance(path, tuple):
self.addfile(*path)
else:
self.addfile(path)
# close
def close(self) :
self.zfile.close()
def extract_to(self, path) :
for p in self.zfile.namelist():
self.extract(p, path)
def extract(self, filename, path) :
if not filename.endswith("/"):
f = os.path.join(path, filename)
dir = os.path.dirname(f)
if not os.path.exists(dir):
os.makedirs(dir)
self.zfile(f, "wb").write(self.zfile.read(filename))
def create(zfile, files) :
z = ZFile(zfile, 'w')
z.addfiles(files)
z.close()
def extract(zfile, path) :
z = ZFile(zfile)
z.extract_to(path)
z.close()
Copy the code
Recursive algorithm principle
Recursion is a common way to solve a problem, which is to simplify the problem gradually.
The idea is to “call itself,” a method that uses recursive techniques that call itself directly or indirectly.
The recursive structure is divided into two parts:
- ** defines recursion headers: ** without a header will enter an infinite loop, which is also the end condition of recursion
- ** recursive body: ** calls its own method
【 operation 】 find n!
def factorial(n) :
if n == 1:
return n
else:
return n * factorial(n - 1)
print(factorial(10))
Copy the code
[Operation] Output the directory tree
import os
allFiles = []
def get_all_files(path, level) :
childFiles = os.listdir(path)
for file in childFiles:
filepath = os.path.join(path, file)
if os.path.isdir(filepath):
get_all_files(filepath, level + 1)
allFiles.append("\t" * level + filepath)
get_all_files("works2".0)
for f in reversed(allFiles):
print(f)
Copy the code