python不解压直接提取文件_用python从zip文件中提取内部文件

您应该使用-j(垃圾路径(不要生成目录))修饰符(oldv5.52有它)。这是完整的列表:[die]: unzip(1) - Linux man page,或者您可以在终端中运行(${PATH_TO}/)解压缩,它将输出参数列表。

考虑到您希望提取名为target的文件夹中的文件,请使用命令(您可能需要指定解压缩的路径):"unzip" -j "a.zip" -d "target" "folder1/folder2/inner.txt"

输出(Win,但对于Nix,这是相同的事情):(py35x64_test) c:\Work\Dev\StackOverflow\q047439536>"unzip" -j "a.zip" -d "target" "folder1/folder2/inner.txt"

Archive: a.zip

inflating: target/inner.txt

输出(无-j):(py35x64_test) c:\Work\Dev\StackOverflow\q047439536>"unzip" "a.zip" -d "target" "folder1/folder2/inner.txt"

Archive: a.zip

inflating: target/folder1/folder2/inner.txt

或者,既然你提到了Python

代码.py:import os

from zipfile import ZipFile

def extract_without_folder(arc_name, full_item_name, folder):

with ZipFile(arc_name) as zf:

file_data = zf.read(full_item_name)

with open(os.path.join(folder, os.path.basename(full_item_name)), "wb") as fout:

fout.write(file_data)

if __name__ == "__main__":

extract_without_folder("a.zip", "folder1/folder2/inner.txt", "target")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值