3

Using the zipfile module I have created a script to extract my archived files, but the method is corrupting everything other than txt files.

def unzip(zip):
         filelist = []
         dumpfold = r'M:\SVN_EReportingZones\eReportingZones\data\input\26012012'
         storage = r'M:\SVN_EReportingZones\eReportingZones\data\input\26012012__download_dump'
         file = storage + '\\' + zip
         unpack = dumpfold + '\\' + str(zip)
         print file

         try:

                     time.sleep(1)
                     country = str(zip[:2])
                     countrydir =  dumpfold + '\\' + country
                     folderthere = 0
                     if exists(countrydir):
                        folderthere = 1           

                     if folderthere == 0:
                       os.makedirs(countrydir)

                     zfile = zipfile.ZipFile(file, 'r')
##                     print zf.namelist()
                     time.sleep(1)
                     shapepresent = 0

Here I have a problem - by reading and writing the zipped data, the zipfile command seems to be rendering it unusable by the programs in question - I am trying to unzip shapefiles for use in ArcGIS...

                     for info in zfile.infolist():
                         fname = info.filename
                         data = zfile.read(fname)
                         zfilename = countrydir + '\\' + fname
                         fout = open(zfilename, 'w')# reads and copies the data
                         fout.write(data)
                         fout.close()
                         print 'New file created ----> %s' % zfilename





         except:
                        traceback.print_exc()
                        time.sleep(5)

Would it be possible to call WinRar using a system command and get it to do my unpacking for me? Cheers, Alex

EDIT

Having used the wb method, it works for most of my files but some are still being corrupted. When I used winRar to manually unzip the problematic files they load properly, and they also show a larger ile size.

Please could somebody point me in the direction of loading winRar for the complete unzip process?

Alex Oulton
  • 199
  • 1
  • 4
  • 12

2 Answers2

3

You are opening the file in a text mode. Try:

       fout = open(zfilename, 'wb')# reads and copies the data

The b opens the file in a binary mode, where the runtime libraries don't try to do any newline conversion.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
0

To answer the second section of your question, I suggest the envoy library. To use winRar with envoy:

import envoy
r = envoy.run('unrar e {0}'.format(zfilename))
if r.status_code > 0:
    print r.std_err
print r.std_out

To do it without envoy:

import subprocess
r = subprocess.call('unrar e {0}'.format(zfilename), shell=True)
print "Return code for {0}: {1}".format(zfilename, r)
Spencer Rathbun
  • 14,510
  • 6
  • 54
  • 73
  • Thanks for your answer. Unfortunaly having installed envoy the code spits out this error:Traceback (most recent call last): File "C:\Python26\ArcGIS10.0\lib\threading.py", line 532, in __bootstrap_inner self.run() File "C:\Python26\ArcGIS10.0\lib\threading.py", line 484, in run self.__target(*self.__args, **self.__kwargs) File "build\bdist.win32\egg\envoy\core.py", line 40, in target bufsize=0, File "C:\Python26\ArcGIS10.0\lib\subprocess.py", line 633, in __init__ errread, errwrite) File "C:\Python26\ArcGIS10.0\lib\subprocess.py", line 842, in _execute_child... – Alex Oulton Feb 02 '12 at 14:46
  • more specifically File "M:\SVN_EReportingZones\eReportingZones\data\scripts\file_unzipper_alpha999.py", line 101, in unzip r = envoy.run('unrar e {0}'.format(unpack)) File "build\bdist.win32\egg\envoy\core.py", line 167, in run out, err = cmd.run(data, timeout) File "build\bdist.win32\egg\envoy\core.py", line 52, in run self.returncode = self.process.returncode AttributeError: 'NoneType' object has no attribute 'returncode' – Alex Oulton Feb 02 '12 at 14:50
  • @AlexOulton I've only used envoy with python27. My guess would be envoy is attempting to use functionality that was only added with 2.7, so try the subprocess only version. – Spencer Rathbun Feb 02 '12 at 15:13
  • @AlexOulton After some looking within the envoy repo, I came across [this bug report](https://github.com/kennethreitz/envoy/issues/15). If you open up the envoy source on your machine, You should be able to set `shell=True` for the popen statements. Try that, and see if it fixes your problem. Also make sure that `unrar` is in your path. – Spencer Rathbun Feb 02 '12 at 15:27
  • Hi - I have tried to use the subprocess method but I am getting the following error - 'unrar is not recognised as an internal or external command, operable program or batchfile'. Could you please tell me what I'm going wrong? – Alex Oulton Feb 02 '12 at 16:03
  • 1
    @AlexOulton Have you installed the [unrar addon](http://www.rarlab.com/rar_add.htm)? Is the folder containing the unrar command on your path? [Have you tried using unrar from the cmd prompt](http://www.respower.com/page_tutorial_unrar)? – Spencer Rathbun Feb 02 '12 at 16:10