如何在Windows上用Python调用WinRar?仍有问题

时间:2012-01-27 09:33:53

标签: python windows-7 call winrar

使用zipfile模块我创建了一个脚本来提取我的归档文件,但该方法正在破坏除txt文件之外的所有内容。

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

这里我有一个问题 - 通过读取和写入压缩数据,zipfile命令似乎使其无法被相关程序使用 - 我正在尝试解压缩shapefile以便在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)

是否可以使用系统命令调用WinRar并让它为我解压缩?干杯,亚历克斯

修改

使用wb方法后,它适用于我的大多数文件,但有些仍然被破坏。当我使用winRar手动解压缩它们正确加载的有问题的文件时,它们也会显示更大的ile大小。

有人可以指出我在加载winRar的方向上进行完整的解压缩过程吗?

2 个答案:

答案 0 :(得分:3)

您正在以文本模式打开文件。尝试:

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

b二进制模式打开文件,运行时库不会尝试进行任何换行。

答案 1 :(得分:0)

要回答问题的第二部分,我建议envoy library。与envoy一起使用winRar:

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

没有特使就这样做:

import subprocess
r = subprocess.call('unrar e {0}'.format(zfilename), shell=True)
print "Return code for {0}: {1}".format(zfilename, r)