Python 3 版本中 ZipFile 类的 getinfo() 方法用于获取 ZIP 文件中指定文件的信息。

语法:

ZipFile.getinfo(name)

参数说明:

  • name: 要获取信息的文件名,可以是相对路径或绝对路径。如果文件不存在,则会抛出 KeyError 异常。

返回值:

返回一个 ZipInfo 对象,包含指定文件的各种信息,例如文件名、压缩方式、压缩前大小、压缩后大小、修改时间等。

示例:

以下示例演示了如何使用 getinfo() 方法获取 ZIP 文件中指定文件的信息。

import zipfile

# 打开 ZIP 文件
zip_file = zipfile.ZipFile('example.zip')

# 获取文件信息
file_info = zip_file.getinfo('example.txt')

# 打印文件名、压缩方式、压缩前大小、压缩后大小、修改时间等信息
print('File name:', file_info.filename)
print('Compression method:', file_info.compress_type)
print('File size before compression:', file_info.file_size)
print('File size after compression:', file_info.compress_size)
print('Last modified:', file_info.date_time)

输出结果为:

File name: example.txt
Compression method: 8
File size before compression: 20
File size after compression: 20
Last modified: (2021, 4, 6, 11, 19, 20)
Python 3 ZipFile.getinfo() 方法:获取 ZIP 文件信息

原文地址: https://www.cveoy.top/t/topic/mWXY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录