The ZIP file format is a common archive(文档/档案) and compression(压缩) standard. This module provides tools to create, read, write, append, and list a ZIP file.
想进一步了解可以点击链接查看
zipfile — Work with ZIP archives — Python 3.10.2 documentationhttps://docs.python.org/3/library/zipfile.html
python OSThis module provides a portable way of using operating system dependent functionality.
With this way,you can read or write a file and something about file.
python将数据集解压到目标文件import zipfile #引入文件处理函数,便于文件解压缩
import os #python提供的处理文件和目录的方法
#解压src_path路径下的数据到target_path路径下
def unzip_data(src_path,target_path):
# 判断,如果target_path文件为空则执行如下操作
if(not os.path.isdir(target_path)):
读取该文件
z = zipfile.ZipFile(src_path, 'r')
#将上一行代码中打开的源文件解压到目标文件目录
z.extractall(path=target_path)
z.close()
#调用函数
unzip_data()
zipfile.ZipFile() # Open the ZIP file with mode read 'r', write 'w', exclusive create 'x', or append 'a'
统计解压文件中所有子文件的名字和存储空间的大小ZipFile.extractall(path=None, members=None, pwd=None)
Extract all members from the archive to the current working directory.
注意:代码的可复用与模块化
os.listdir()
Return a list containing the names of the files in the directory.
os.path.splitext(path)
Split the pathname path into a pair (root, ext) such that root + ext == path



