使用python从7zip归档文件中解压选定的文件

2024-09-29 21:34:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用subprocess使用7zip解压选择或压缩文件。我不得不使用这种解包方法而不是zipfile模块,因为有时zipfile会损坏shapefile。我目前的方法是:

try:

    for file in os.listdir(downloads):
        print file
        expression2 = sevenzip + " e " +downloads + '\\' + file + " -oC:\Users\Oulton"
        print expression2

    #os.system(r"C:\Users\Oulton\7z e C:\Users\Oulton\install.zip -oC:\Users\Oulton")
        subprocess.call(expression2)



except:
        time.sleep(3)
        traceback.print_exc()

但这并不方便,因为:

  1. 我只想解压缩某些.shp文件,而不是每个zip文件中的所有其他垃圾文件
  2. 一个shell在每次迭代中都是打开和关闭的,我希望shell始终保持打开状态
  3. 我必须使用手动输入来覆盖使用此方法重复命名的文件

Tags: 文件方法osdownloadszipshellusersfile
1条回答
网友
1楼 · 发布于 2024-09-29 21:34:00
  1. 7z e C:\Users\Oulton\install.zip -oC:\Users\Oulton" *.shp -r
  2. 使用Windows的for循环重用同一shell:http://www.robvanderwoude.com/for.php

3。在

-ao (Overwrite mode) switch
Specifies the overwrite mode during extraction, to overwrite files already present on disk.

-i-x可分别用于包含或排除特定文件以进行提取。在

^{pr2}$

相关问题 更多 >

    热门问题