首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用python读取7z文件的内容

如何使用python读取7z文件的内容
EN

Stack Overflow用户
提问于 2015-09-26 21:45:21
回答 5查看 75K关注 0票数 30

如何读取和保存7z的内容。我使用python 2.7.9,我可以像这样提取或归档,但我不能读取Python中的内容,我只列出CMD格式的文件内容

代码语言:javascript
复制
import subprocess
import os

source = 'filename.7z'
directory = 'C:\Directory'
pw = '123456'
subprocess.call(r'"C:\Program Files (x86)\7-Zip\7z.exe" x '+source +' -o'+directory+' -p'+pw)
EN

回答 5

Stack Overflow用户

发布于 2020-07-21 18:38:31

如果你能使用Python3,有一个很有用的库,py7zr,它支持7zip压缩,解压缩,加密和解密。

代码语言:javascript
复制
import py7zr
with py7zr.SevenZipFile('sample.7z', mode='r') as z:
    z.extractall()
票数 14
EN

Stack Overflow用户

发布于 2018-12-02 06:12:27

在这种情况下,我被迫使用7z,并且还需要确切地知道从每个zip归档文件中提取了哪些文件。要处理此问题,您可以检查对7z的调用的输出,并查找文件名。下面是7z的输出:

代码语言:javascript
复制
$ 7z l sample.zip

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=utf8,Utf16=on,HugeFiles=on,64 bits,8 CPUs x64)

Scanning the drive for archives:
1 file, 472 bytes (1 KiB)

Listing archive: sample.zip

--
Path = sample.zip
Type = zip
Physical Size = 472

   Date      Time    Attr         Size   Compressed  Name
------------------- ----- ------------ ------------  ------------------------
2018-12-01 17:09:59 .....            0            0  sample1.txt
2018-12-01 17:10:01 .....            0            0  sample2.txt
2018-12-01 17:10:03 .....            0            0  sample3.txt
------------------- ----- ------------ ------------  ------------------------
2018-12-01 17:10:03                  0            0  3 files

以及如何使用python解析该输出:

代码语言:javascript
复制
import subprocess

def find_header(split_line):
    return 'Name' in split_line and 'Date' in split_line

def all_hyphens(line):
    return set(line) == set('-')

def parse_lines(lines):
    found_header = False
    found_first_hyphens = False
    files = []
    for line in lines:

        # After the header is a row of hyphens
        # and the data ends with a row of hyphens
        if found_header:
            is_hyphen = all_hyphens(''.join(line.split()))

            if not found_first_hyphens:
                found_first_hyphens = True
                # now the data starts
                continue

            # Finding a second row of hyphens means we're done
            if found_first_hyphens and is_hyphen:
                return files

        split_line = line.split()

        # Check for the column headers
        if find_header(split_line):
            found_header=True
            continue

        if found_header and found_first_hyphens:
            files.append(split_line[-1])
            continue

    raise ValueError("We parsed this zipfile without finding a second row of hyphens")



byte_result=subprocess.check_output('7z l sample.zip', shell=True)
str_result = byte_result.decode('utf-8')
line_result = str_result.splitlines()
files = parse_lines(line_result)
票数 7
EN

Stack Overflow用户

发布于 2015-09-26 22:02:44

您可以使用libarchivepylzma。如果您可以升级到python3.3+,则可以使用标准库中的lzma

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32797851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档