1

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
Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
Alex Oulton
  • 199
  • 1
  • 4
  • 12
  • "On occasion the zipfile [module] corrupts shapefiles"? Is this really the case? It seems that `zipfile` is exactly what you want -- I'd recheck this conclusion. Or see if there's a bugfix available if it really is an issue in Python. – Managu Feb 03 '12 at 15:29
  • Are you on Windows? If so, are you reading/writing files in binary mode? [This](http://stackoverflow.com/questions/7082454/zipfile-python-module-bytesize-difference) question might be relevant. – Managu Feb 03 '12 at 15:30
  • @Managu thanks for the tip however yes even when writing binary certain shapefiles have become corrupted strangely - I repeated the process several times and it was always the same shapefiles being corrupted, which are unpacked without incident using cmd 7z – Alex Oulton Feb 03 '12 at 15:38

1 Answers1

1
  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
Uku Loskit
  • 40,868
  • 9
  • 92
  • 93