使用 python 使用子进程从 7zip 存档中解压选定的文件

发布于 2025-01-02 04:45:34 字数 663 浏览 2 评论 0原文

我目前正在使用 subprocess 使用 7zip 解压选择的文件或 zip 文件。我必须使用这种解包方法而不是 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. 我必须使用手动输入来使用此方法覆盖重复命名的文件

I am currently using subprocess to unpack a selection or zip files using 7zip. I have to use this unpacking method instead of the zipfile module because on occasion the zipfile corrupts shapefiles. My current method is:

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()

But this is not convenient because:

  1. I only want to unpack certain .shp files and not all of the other junk in each zip
  2. A shell is opened and closed for each iteration, I would prefer the shell to stay open throughout
  3. I have to use manual input to overwrite duplicately named files using this method

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凤舞天涯 2025-01-09 04:45:34
  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 可用于分别包含或排除要提取的特定文件。

7z e C:\Users\Oulton\install.zip -oC:\Users\Oulton -ir!*.shp -ir!*.mxd -ir!*.shx -ir!*.sbn -ir!*.dbf -ir!*.xml
  1. 7z e C:\Users\Oulton\install.zip -oC:\Users\Oulton" *.shp -r
  2. Use Windows's for loops to reuse the same 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 and -x can be used to respectively include or exclude specific files for extraction.

7z e C:\Users\Oulton\install.zip -oC:\Users\Oulton -ir!*.shp -ir!*.mxd -ir!*.shx -ir!*.sbn -ir!*.dbf -ir!*.xml
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文