python中的zipfile

zipfile — Work with ZIP archives

ZipFile.namelist()
Return a list of archive members by name.
返回压缩成员名称列表

with ZipFile('spam.zip', 'r') as myzip:
    for f in myzip.namelist():
        print(f)


ZipFile.getinfo(name)
Return a ZipInfo object with information about the archive member name. Calling getinfo() for a name not currently contained in the archive will raise a KeyError.
根据成员名称获取ZipInfo

ZipFile.infolist()
Return a list containing a ZipInfo object for each member of the archive. The objects are in the same order as their entries in the actual ZIP file on disk if an existing archive was opened.
返回ZipInfo列表

ZipFile.extract(member, path=None, pwd=None)
ZipFile.extractall(path=None, members=None, pwd=None)
解压缩文件,path指定路径(默认为当前路径)

ZipFile.write(filename, arcname=None, compress_type=None)
ZipFile.writestr(zinfo_or_arcname, bytes[, compress_type])
写入, arcname压缩后成员名称
with ZipFile('spam.zip', 'w') as myzip:
    myzip.write('eggs.txt')


原文地址:https://www.cnblogs.com/xkxjy/p/3672256.html