使用Python通过subprocess解压7zip档案中的选定文件
我现在正在用subprocess这个工具来解压一些选择的zip文件,使用的是7zip。我必须用这种解压方法,因为有时候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()
但是这样做不太方便,因为:
- 我只想解压某些.shp文件,而不是每个zip里的所有其他杂七杂八的东西
- 每次操作都会打开和关闭一个命令行窗口,我希望这个窗口能一直开着
- 用这种方法我还得手动输入来覆盖那些名字重复的文件
1 个回答
1
7z e C:\Users\Oulton\install.zip -oC:\Users\Oulton" *.shp -r
- 使用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