2012-02-03 228 views
1

我目前使用子进程使用7zip解压缩选择或zip文件。我必须使用这种解压方法而不是zipfile模块,因为有时zipfile会破坏shapefile。我现在的方法是:使用python从7zip压缩文件中使用子进程解压缩选定的文件

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文件,而不是所有其他垃圾的每个拉链
  2. 壳是每次迭代打开和关闭,我宁愿壳始终保持打开
  3. 我必须使用手动输入来覆盖使用此方法重复命名的文件
+0

“有时zipfile [module]会破坏shapefile”?这是真的吗?看起来'zipfile'正是你想要的 - 我会重新检查这个结论。或者看看是否有可用的错误修复,如果它确实是Python中的问题。 – Managu 2012-02-03 15:29:15

+0

你在Windows吗?如果是这样,你是否在二进制模式下读/写文件? [这](http://stackoverflow.com/questions/7082454/zipfile-python-module-bytesize-difference)问题可能是相关的。 – Managu 2012-02-03 15:30:52

+0

@Managu感谢提示,但是是的,即使在编写二进制文件时,某些形状文件奇怪地变得破损 - 我重复了几次这个过程,并且它始终是相同的形状文件被损坏的,这些文件没有使用cmd 7z – 2012-02-03 15:38:48

回答

1
  1. 7z e C:\Users\Oulton\install.zip -oC:\Users\Oulton" *.shp -r
  2. 使用Windows的for循环重复使用相同的外壳: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 
+0

谢谢帮助,为了从每个zip文件中提取某些特定的文件,你可以提供任何见解? – 2012-02-03 15:49:46

+0

你知道文件名和目录结构吗? – 2012-02-03 15:55:36

+0

是的,我只想提取以[.shp .mxd .shx .sbn .dbf .xml] – 2012-02-03 16:13:07