如何使用python读取7z文件的内容 [英] How to read contents of 7z file using python

查看:67
本文介绍了如何使用python读取7z文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何读取和保存 7z 的内容.我使用Python 2.7.9,我可以像这样提取或存档,但我无法在python中读取内容,我只在CMD中列出文件的内容

How can I read and save the contents of 7z. I use Python 2.7.9, I can extract or Archive like this, but I can't read contents in python, I only listing the file's contents in CMD

import subprocess
import os

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

推荐答案

如果你可以使用 python 3,有一个有用的库,py7zr,支持7zip存档压缩、解压、加解密.

If you can use python 3, there is a useful library, py7zr, which supports 7zip archive compression, decompression, encryption and decryption.

import py7zr
with py7zr.SevenZipFile('sample.7z', mode='r') as z:
    z.extractall()

这篇关于如何使用python读取7z文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆