current position:Home>Python beginner's eighth day ()
Python beginner's eighth day ()
2022-01-30 02:39:42 【Sue_ fifty-two】
Python Advanced -- Document processing (IO technology )
A complete program generally includes data storage and reading ; In actual development, we often need external storage media ( Hard disk 、 Compact disc 、U Plate, etc. ) Reading data , Or store the data generated by the program in a file “ Persistence ” preservation .
Text files and binaries
- text file The text file stores ordinary “ character ” Text , The default is unicode Character set , You can use Notepad to open .
- Binary Binary files use the data content as “ byte ” For storage , Can't open with Notepad . The corresponding software must be used to decode ; Such as :MP4、Mp3、JPG、doc Documents, etc. .
File operation related modules
name | explain |
---|---|
IO modular | Input and output operations of file stream |
OS modular | Enter this operating system function , Including file operations |
glob modular | Find a file pathname that matches a specific rule |
fnmatch modular | Use modules to match file pathnames |
fileinput modular | Process multiple input files |
filecmp modular | For file comparison |
cvs modular | be used for cvs Document processing |
pickle and cPickle | For serialization and deserialization |
xml package | be used for XML Data processing |
bz2、gzip、zipfile、zlib、tarfile | For processing compressed and decompressed files ( Corresponding to different algorithms ) |
Create a file object open()
open()
Function to create a file object , Basic grammar :
open( file name [, Open mode ])
Copy code
If it's just the file name , Represents the file in the current directory . The file name can be entered in the full path, such as :D:\a\b.txt .
Open mode :
Pattern | describe |
---|---|
r | Reading mode |
w | Write mode , If the file does not exist, create ; Rewrite the new content if it exists |
a | Append mode . If the file does not exist, it will be created. Otherwise, the content will be added at the end of the file |
b | Binary mode ( Can be used in combination with other modes ) |
+ | read 、 Write mode ( ditto ) |
Basic file writing and other operations
Three steps :
- Create a file object
- Write data
- Close file object
【 operation 】 Write data
# Absolute path
# file = open(
# "D:/00.study/Study-Python/02.Python Advanced /day02 - Python Advanced /code/file.txt",
# "a",
# encoding="utf-8")
# a = " wuhu !!!"
# file.write(a)
# file.close()
# Relative paths ( Need to change vscode To configure )
file = open("file.txt", "a", encoding="utf-8")
a = " wuhu !!!"
file.write(a)
file.close()
Copy code
Introduction to common codes
When operating Chinese , Will encounter garbled code problems . Coding relationships :
Chinese code scrambling
window The default operating system code is GBK,Linux The default operating system code is UTF-8. When we use open()
when , What is called is the folder opened by the operating system , No default code is GBK.
write() / writeLines() Write data
write(a): Put the string a Write to a file writelines(b): Write the string list to the file , Do not add line breaks ( You can add ).
【 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 code
result :file.txt
sue
Jason
Eason
Forrest
Copy code
close() Close file stream
The bottom layer of the file is controlled by the operating system , Therefore, to open the file object, you must first display the call close() Method to close the file object . When calling close() When the method is used , First, the buffer data will be written to the file ( It can also be called directly flush() Method ), Close the file again , Release file object .
To ensure that the open file object is closed properly , Generally combined with the exception mechanism finally
perhaps with
Keyword implementation can close open file objects in any case .
【 operation 】 Use exceptions and combine finally Make sure you close the file
try:
file = open(r"file.txt", "a", encoding="utf-8")
str = "Sue, Great commander !!!\n"
file.write(str)
except BaseException as e:
print(e)
finally:
file.close()
Copy code
with sentence ( Context management )
with keyword ( Context manager ) Context resources can be managed automatically , Jump out for whatever reason with modular , Can ensure that the file is closed normally , And it can automatically restore the scene when entering the code block after 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 code
with The opening and closing operations are carried out , We don't have to close it ourselves
Text file reading
There are three general methods for reading files :
- read([size]) Read... In a file size Characters , And return as a result . without size Parameters , Read the entire file . Read to the end of the file , Returns an empty string .
- readline() Read a line and return as a result . Read to the end of the file , Returns an empty string .
- readlines() In the text file , Each line is stored in the list as a string , Return to the list .
【 operation 】read() Method
with open(r"file.txt", "r", encoding="utf-8") as f:
print(f.read(10)) # sue,Jason,
Copy code
【 operation 】readline() Method
with open(r"file.txt", "r", encoding="utf-8") as f:
print(f.readline()) # sue,Jason,Eason,Forrest,primary,middle,high
Copy code
【 operation 】readlines() Method
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 code
【 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 code
Reading and writing binary files
The processing flow of binary file is consistent with that of text file . First, create a file object , However, it needs to be specified as binary mode , To create a binary object .
Example :
file = open(r"xxx.txt","wb") # Can write 、 Rewrite the binary object of the schema
file = open(r"xxx.txt","ab") # Can write 、 Append mode binary object
file = open(r"xxx.txt","rb") # Readable binary object
Copy code
After creation, you can still use write()、read() Read and write files
【 operation 】 Copy a binary file
with open(r"cat.gif", "rb") as f: # Binary file to copy
with open(r"copy_one.gif", "wb") as w: # The copied binary
for line in f.readlines():
w.write(line)
print(" Copy end ")
Copy code
Common properties and methods of file objects
File objects encapsulate file related operations . The following example illustrates the common properties and methods
attribute :
attribute | explain |
---|---|
name | Returns the name of the file |
mode | Returns the open mode of the file |
closed | If the file is closed, return True |
Open mode of file object :
Pattern | explain |
---|---|
r | Read instructions , If the file does not exist, an exception is thrown |
w | Write mode , If the file exists, the original |
x | Write mode , Create a new file , Throw an exception if the file exists |
a | Append mode , Do not overwrite the original contents of the file |
b | Binary mode ( Can be combined with other modes ) |
t | Text mode ( Default , Omission ) |
+ | Read write mode ( Can be combined with other modes ) |
Common methods of file objects :
file name | explain |
---|---|
read([size]) | Read from file size The contents of bytes or characters return . If you omit size Then read all |
readline() | Read a line from a text file |
readlines() | Take each line read from the text file as an independent string object , And put these objects in the list to return |
write(str) | The string str Content written to file |
writelines(s) | The string list s write file , Do not add line breaks |
seek(offset[,whence]) | Move the file pointer to a new location ,offset For whence The location of : offset: off To move towards the end , If it is negative, it moves in the starting direction whence Different stands for different meanings : 0: Calculate from the file header ( The default value is ) 1: From the current position 2: Calculate from the end of the file |
tell() | Returns the current location of the file pointer |
flush() | Write the contents of the buffer to the file , But do not close the file |
close() | Write the contents of the buffer to the file , Close the file at the same time , And release the file object |
truncate([size]) | Delete the contents from the current pointer position to the end of the file . If you specify size, Then no matter where the pointer is, only the front size Bytes , Delete the rest |
writeable() | Test whether the current file is writable |
readable() | Test whether the current file is readable |
【 operation 】 test seek Methods and name、mode、closed attribute
with open(r"file.txt", "r", encoding="utf-8") as f:
f.seek(2) # Indexes --- The pointer
print(f.read())
# e,---1
# Jason,---2
# Eason,---3
# Forrest,---4
# primary,---5
# middle,---6
# high,---7
print(f.name) # file.txt
print(f.mode) # r
print(f.closed) # False
print(f.closed) # True
Copy code
【 operation 】 test truncate、flush Method
# -------------flush------------------
file = open(r"file.txt", "r", encoding="utf-8")
file.flush() # write It's not written directly , The data will also be put into the 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 code
pickle serialize
Python in , The essence of an object is a “ Memory block for storing data ” . When we need to save the data in the memory block to the hard disk , Or when transmitted to other computers through the network . We just need “ Serialization and deserialization of objects ”. The application of serialization mechanism in distributed system 、 On parallel systems .
It can realize the object in memory and the data format which is convenient to persist in disk or network (str、bites) Mutual conversion between . This mechanism is called serialization and serialization
serialize : The process of converting nonpersistent and transitive objects in memory into objects that can be persisted and transitioned easily .
Deserialization : The process of converting persistent and transitive objects to non persistent and transitive objects .
pickle Modular dumps()、dump()、loads()、load() A function can be divided into two groups according to its function : serialize :dumps()、dump() Deserialization :loads()、load()
Grammar format :
pickle.dump(obj,file) # obj Is the object to be serialized ,file Refers to storing files
pickle.load(file) # from file Reading data , Anti serialization into objects .
Copy code
【 operation 】 Serialize the object into 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 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 code
CSV File operations
CSV(Cooma Separated Values)
Is the comma separator text format , Commonly used for data exchange 、Excel Import and export of file and database data . And Excel Different documents , stay CSV In file :
- There is no type of value , All values are strings
- Font color and style cannot be specified
- You cannot specify the width and height of a cell , Can't merge cells
- There are no multiple worksheets
- You can't embed an image chart
Excel The form created in the form is :
Save as “csv” Format , The contents after opening are as follows :
CSV Read and write files
CSV
File reading
Remember to introduce... Before use csv package
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 code
adopt csv.reader() Function to create an object that reads data
CSV
Writing files
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 code
OS modular ()
OS Module allows us to control the operating system directly . Directly call the executable file of the operating system 、 command , Operation file 、 Directory etc. . It is the core foundation of system operation and maintenance
OS Call operating system files and commands
os.system
system Function to convert a string into a command to run on the server ; The principle is that every system Function execution time , It creates a subprocess to execute the command line on the system , The execution result of a child process cannot affect the main process ;
【 operation 】 Open Notepad
import os
os.system("notepad.exe")
Copy code
【 operation 】 Calling system's ping command
os.system("ping www.google.com")
# Pinging www.google.com [157.240.12.35] with 32 bytes of data:
# Request timed out. ...
Copy code
【 operation 】 open cmd and powershell
os.system("cmd")
os.system("powershell")
Copy code
os.startfile
You can open the file according to the root directory of the file
os.startfile(r"D:/WeChat/WeChat.exe")
Copy code
OS modular - File and directory operations
If you need to do other operations on files and directories , have access to os
and os.path
modular
os Common file operations of the module :
Method name | describe |
---|---|
remove(path) | Delete the specified file |
rename(src,dest) | Rename a file or directory |
stat(path) | Returns all properties of the file |
listdir(path) | return path List of files and directories under the directory |
Use :
os.rename("./cat.gif", "rename_cat.gif")
Copy code
os.remove("./file1.txt")
Copy 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 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-Deeper', 'Study-Python', 'vue-manage-system-master', ' Homework ', ' Review documents ']
print(os.name) # nt --> windows; linux&unix -->posix
Copy code
os Related methods of directory operation under module :
Method name | describe |
---|---|
mkdir(path) | Create directory |
makedirs(path1/path2/...) | Create multi-level directory |
rmdir(path) | Delete directory |
removedirs(path1/path2/...) | Delete multi-level directory |
getcwd() | Return to the current working directory :current work dir |
chdir(path) | hold path Set as current working directory |
walk() | Traverse directory tree |
sep | The path separator used by the current operating system |
Use :
os.mkdir("works")
Copy code
os.makedirs("works/jobs/stu")
Copy code
os.rmdir("works")
Copy code
os.removedirs("works/jobs/stu")
Copy code
os.getcwd()
os.chdir("xxx")
os.walk()
Copy code
os.path modular - Directory operation
os.path The module provides directory related information ( Path judgment 、 Path segmentation 、 Path link 、 Folder traversal ) The operation of
Method | describe |
---|---|
isabs(path) | Judge path Is it an absolute path |
isdir(path) | Judge path Is it a directory |
sifile(path) | Judge path Is it a document |
exists(path) | Judge whether the file in 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 method traverses the directory |
join(path,*paths) | Connect multiple path |
split(path) | Split the path , Return... As a list |
splitext(path) | Split the file extension from the 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 Advanced \day02 - Python Advanced \code\file.txt
print(os.path.dirname(__file__))
# d:\00.study\Study-Python\02.Python Advanced \day02 - Python Advanced \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 code
walk() Recursively traverses all files
os.walk() Method :
Return to one 3 Tuples of elements .(dirpath,dirnames、filenames):
- dirpath: List the path of the specified directory
- dirnames: Catalog all your favorite folders
- filenames: All the files in the directory
Used to output the file name in the directory by swimming away in the directory tree , Up or down . An easy-to-use file 、 Directory traverser , It can help us process files efficiently 、 About the catalogue .
walk() The syntax format of the method is as follows :
os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
Copy code
Parameters :
- top -- Is the address of the directory you want to traverse , Returns a triple (root,dirs,files).
- root It refers to the address of the folder being traversed
- dirs It's a list , The contents are the names of all the directories in the folder ( Exclude subdirectories )
- files The same is list , The content is all the files in this folder ( Exclude subdirectories )
- topdown -- Optional , by True, Then the priority traverses top Catalog , Otherwise, priority traversal top A subdirectory ( On by default ). If topdown Parameter is True,walk Can traverse top Folder , And top Every subdirectory in the folder .
- onerror-- Optional , Need one callable object , When walk When exception is required , Would call .
- followlinks -- Optional , If True, Will traverse the shortcut under the directory (linux Next is the soft connection symbolic link ) The actual Directory ( Off by default ), If False, Then the priority traverses top A subdirectory .
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 code
shutil modular ( Copy and compress )
shutil The module is python Provided in the standard library , Mainly for copying files and folders Move 、 Delete etc. ; You can also compress files and folders 、 Decompression operation .
os Module provides general operations on directories or files ,shutil Module as a supplement , Provides mobile 、 Copy 、 Compress 、 Decompression and other operations
Method | describe |
---|---|
copyfileobj(src, dst) | Cyclic reading old.txt File contents and write to new.txt The file of |
shutil.copyfile(src, dct) | From source src Copied to the dst In the middle . Of course, the premise is that the target address has writable permission . The exception information thrown is IOException. If the current dst If it already exists, it will be covered |
shutil.copymode(src, dst) | It will only copy its permissions, and other things will not be copied |
shutil.copystat(src, dst) | Copy permission 、 Last access time 、 Last modified |
shutil.copy( src, dst) | Copy a file to a file or a directory |
shutil.copy2( src, dst) | stay copy Based on the above, the last access time and modification time of the copied file are also copied , Be similar to cp –p Things that are |
shutil.copy2( src, dst) | If the file systems of the two locations are the same, it is equivalent to rename operation , It's just a change of name ; If it is not in the same file system, it is to do move operation |
shutil.copytree( olddir, newdir, True/Flase) | hold olddir Copy a newdir, If the first 3 The parameters are True, Then the symbolic connection under the folder will be maintained when copying the directory , If the first 3 The parameters are False, Physical copies will be generated in the replicated directory to replace symbolic connections |
shutil.rmtree( src ) | Recursively delete a directory and all contents in the directory |
shutil.ignore_patterns(*patterns) | Ignore which file , Selective copy |
shutil.make_archive(base_name, format[, root_dir[, base_dir[, verbose[, dry_run[, owner[, group[, logger]]]]]]]) | Create a zip and return the file path , for example :zip、tar |
Use :
shutil.copyfileobj(src, dst):
shutil.copyfileobj(open("old.txt", "r"), open("new.txt", "w"))
os.system("cat old.txt new.txt")
Copy code
Must take open("xxxx","x") Open the file and write , Otherwise, the report will be wrong copyfileobj Method will only copy the contents of the file
shutil.copyfile(src, dct):
shutil.copyfile("old.txt", "new1.txt")
os.system("cat old.txt new.txt")
Copy code
From source src Copied to the dst In the middle . Of course, the premise is that the target address has writable permission . The exception information thrown is IOException. If the current dst If it already exists, it will be covered If there is a file, the contents of the file will be overwritten , Create if not
*shutil.copymode(src, dst, , follow_symlinks=True):
shutil.copymode("old.txt", "new1.txt")
Copy code
Copy only file permissions , The content of the document 、 Group 、 No change in users
*shutil.copystat(src, dst, , follow_symlinks=True):
shutil.copystat("old.txt", "new1.txt")
os.system("stat old.txt new.txt")
Copy code
Copy file status information , The file must exist , No copy Change the time
shutil.copy(src, dst, *, follow_symlinks=True):
shutil.copy("old.txt", "new2.txt")
os.system("stat old.txt new2.txt")
Copy code
Copy files and status information , Same no copy Change the time
shutil.copy2(src,dct):
shutil.copy("old.txt", "new2.txt")
os.system("stat old.txt new2.txt")
Copy 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 code
Copy folder recursively works2 Directory must not exist ,symlinks=True only copy Link to the file , If it is equal to False Just copy Source file ,ignore Equal to No copy A file or directory
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 code
shutil.move(src, dst, copy_function=copy2):
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 code
Recursively remove files , It is similar to mv command , Actually, it's renaming
shutil.make_archive(base_name, format[, root_dir[, base_dir[, verbose[, dry_run[, owner[, group[, logger]]]]]]]):
base_name: Filename of the package , It can also be the path of the compressed package . When it's just a filename , Save to current directory , Otherwise save to the specified path , Such as : tar_name => Save to current path Such as :/Users/a6/tar_name => Save to /Users/a6/ format: Type of compression package ,“zip”, “tar”, “bztar”,“gztar” root_dir: Folder path to compress ( Default current directory ) owner: user , Default current user group: Group , Default current group logger: For logging , Usually logging.Logger object
zipFile modular ( decompression )
zipfile
yes python It's used to make zip Format encoding compression and decompression , Because it is very common zip Format , Therefore, the frequency of using this module is also relatively high .
Common methods :
Method | describe |
---|---|
is_zipfile( file ) | test filename The file of , See if it works zipfile |
ZipFile(filename[,mode[,compression[,allowZip64]]]) | - filename: File object - mode: Optional r、w、a Open style representing different documents ;r read-only ;w rewrite ;a add to - compression: Point out that zipfile With what compression method , The default is :ZIP_STORED, Another option is :ZIP_DEFLATED - allowZip64: It's a bool Type variable , When set to True It can be used to create a file with a size greater than 2G Of zip file , The default is :True |
close() | close zip file , You must write |
extract(member,path=None,pwd=None) | from zip Extract a file from |
extractall(path[,pwd]) | File as namelist From the current directory structure zip Extract it from and put it in the specified directory ; these two items. extract Of path If it doesn't exist, it will be created automatically , And this path It must be a directory , When decompressing, it must be a file , Including its relative zip All the directories of the package path are extracted together . |
namelist() | Return a list , The content is zip Of all the sub files in the file path( be relative to zip In terms of packages ). It's like a preserved one zip List of internal directory structures |
infolist() | Go back to a list , The content is every zip A file is a sub file ZipInfo object , This object has the fields mentioned above |
printdir() | take zip The directory structure of the file is printed to stdout On , Include... For each file path, Modification time and size |
open(name[,mode[,pwd]]) | Get the file object of a child file , It can be used to read,readline,write Operation, etc. |
setpassword(psw) | Set up zip The password of the file |
testzip() | Read zip All files in , Verify their CRC The checksum . Returns the name of the first corrupt file , If all the files are complete, return None |
write(filename[,arcname[,compression_type]]) | take zip External documents filename Write to a file named arcname In the sub file of ( Of course arcname Also with relative zip The path of the package is ),compression_type Specifies the compression format , It's also ZIP_STORED or ZIP_DEFLATED.z The way you open it must be w perhaps a To write to the file |
Operation and use :
# zipFile Use of modules
import zipfile
# 1. is_zipfile Check whether 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() Close file , There has to be... At the end
z = zipfile.ZipFile("works2.zip", "r")
z.close()
print(z) # <zipfile.ZipFile [closed]>
# 4. extract(member,path=None,pwd=None) from zip Extract a file from
print(z.extract("works2/s.txt", path=None, pwd=None))
# 5. extractall(path[,pwd]) File as namelist From the current directory structure zip Extract it from and put it in the specified directory
z.extractall("works2")
z.close()
# 6. namelist() Return a list , The content is zip Of all the sub files in the file path
print(z.namelist()) # ['works2/', 'works2/some/', 'works2/some/www/']
z.close()
# 7. infolist() Return a list , The content is every zip A file is a sub file ZipInfo object
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() take zip The directory structure of the file is printed to stdout On , It includes path, Modification 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]]), Get the file object of a child file , It can be used to read、readline、write Wait for the operation
# 10. setpassword(psw), Set up zip The password of the file
z.setpassword(123456)
# 11. testzip() Read zip All documents in
a = z.testzip()
print(a is None)
# 12. write(filename[,arcname[,compression_type]]) Write it to the file zip in
z = zipfile.ZipFile("works2.zip", "w")
z.write("files.py")
z.write("os.py")
z.write("shutil.py")
z.close()
Copy code
Some ways of using :
- Unzip the file and read
import zipfile
z = zipfile.ZipFile(filename, 'r') # The second parameter here is r It means reading zip file ,w Is to create a zip file
for f in z.namelist():
print(f)
Copy code
- Compress into file and write
import zipfile, os
z = zipfile.ZipFile(filename, 'w') # Note that the second parameter here is w, there filename Is the name of the compressed package
# Suppose you want to call one testdir All the files in are added to the compressed package ( Only the files in the first level subdirectory are added here ):
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 code
One click compression function :
# Import the package needed for compression
import zipfile
# Import os modular
import os
# Import os.path modular
import os.path
# 1. Create a class
class ZFile(object):
# 2. Initialization data filename- file name ;mode- The way ;basedir- Absolute path
def __init__(self, filename, mode="r", basedir=""):
self.filename = filename
self.mode = mode
# If the writing method is w- write in ,a- Additional
if self.mode in ("w", "a"):
# Read zip File and set
self.zfile = zipfile.ZipFile(filename,
self.mode,
compression=zipfile.ZIP_DEFLATED)
else:
# without , Default
self.zfile = zipfile.ZipFile(filename, self.mode)
# Absolute path
self.basedir = basedir
# If you don't write , Then use os.path.dirname Automatic completion
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 code
Principle of recursive algorithm
recursive , Is a common way to solve problems , That is to simplify the problem gradually .
The idea is “ Call yourself ” A method using recursive technology will call itself directly or indirectly .
The recursive structure is divided into two parts :
- ** Define recursive headers :** Without a head, you will enter a dead cycle , This is also the end condition of recursion
- ** Recursive body :** Call your own method
【 operation 】 seek n!
def factorial(n):
if n == 1:
return n
else:
return n * factorial(n - 1)
print(factorial(10))
Copy code
【 operation 】 Output 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 code
copyright notice
author[Sue_ fifty-two],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201300239376212.html
The sidebar is recommended
- Getting started with Python - object oriented - special methods
- Using linear systems in python with scipy.linalg
- Fast power modulus Python implementation of large numbers
- Python architects recommend the book "Python programmer's Guide" which must be read by self-study Python architects. You are welcome to take it away
- Decoding the verification code of Taobao slider with Python + selenium, the road of information security
- Python game development, pyGame module, python implementation of skiing games
- Python collects and monitors system data -- psutil
- Python + selenium automated test: page object mode
- You can easily get started with Excel. Python data analysis package pandas (IV): any grouping score bar
- Python ThreadPoolExecutor restrictions_ work_ Queue size
guess what you like
-
Python generates and deploys verification codes with one click (Django)
-
[Python kaggle] pandas basic exercises in machine learning series (6)
-
Using linear systems in python with scipy.linalg
-
Using Python to realize national second-hand housing data capture + map display
-
How to make Python run faster? Six tips!
-
Python chat room (Tkinter writing interface, streaming, socket to realize private chat, group chat, check chat records, Mysql to store data)
-
This pandas exercise must be successfully won
-
[algorithm learning] sword finger offer 64 Find 1 + 2 +... + n (Java / C / C + + / Python / go / trust)
-
Understand Python's built-in function and add a print function yourself
-
Python implements JS encryption algorithm in thousands of music websites
Random recommended
- leetcode 35. Search Insert Position(python)
- [introduction to Python visualization]: 12 small examples of complete data visualization, taking you to play with visualization ~
- Learning this Python library can reduce at least 100 lines of code
- leetcode 67. Add Binary(python)
- Regular re parameter replacement of Python 3 interface automation test framework
- V. pandas based on Python
- Only 15 lines of code is needed for face detection! (using Python and openCV)
- [Python crawler Sao operation] you can crawl Sirius cinema movies without paying
- leetcode 69. Sqrt(x)(python)
- Teach you to read the source code of Cpython (I)
- Snowball learning started in the fourth quarter of Python. One needs three meals. I have a new understanding of Python functional programming, process-oriented, object-oriented and functional
- leetcode 88. Merge Sorted Array(python)
- Don't you know more about a python library before the end of 2021?
- Python crawler web page parsing artifact XPath quick start teaching!!!
- Use Python and OpenCV to watermark the image
- String and related methods of Python data type introduction
- Heapq module of Python module
- Introduction to beautiful soup of Python crawler weapon, detailed explanation, actual combat summary!!!
- Event loop of Python collaboration series
- Django docking pin login system
- [recalling the 1970s] using Python to repair the wonderful memories of parents' generation, black-and-white photos become color photos
- You used to know Python advanced
- Pyinstaller package Python project
- 2021 IEEE programming language rankings: Python tops the list!
- Implementation of Python automatic test control
- Python advanced: [Baidu translation reverse] graphic and video teaching!!!
- Do you know the fuzzy semantics in Python syntax?
- [Python from introduction to mastery] (XXVII) learn more about pilot!
- Playing excel office automation with Python
- Some applications of heapq module of Python module
- Python and go languages are so popular, which is more suitable for you?
- Python practical skills task segmentation
- Python simulated Login, numpy module, python simulated epidemic spread
- Python opencv contour discovery function based on image edge extraction
- Application of Hoff circle detection in Python opencv
- Python reptile test ox knife (I)
- Day 1: learn the Django framework of Python development
- django -- minio_ S3 file storage service
- [algorithm learning] 02.03 Delete intermediate nodes (Java / C / C + + / Python / go)
- Learning in Python + opencv -- extracting corners