2012-02-03 68 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

相關問題