3

使用 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 手动解压缩有问题的文件时,它们会正确加载,并且它们还会显示更大的文件大小。

请有人指出我加载winRar的方向以完成完整的解压缩过程吗?

4

2 回答 2

3

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

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

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

于 2012-01-27T09:45:44.080 回答
0

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

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)
于 2012-02-02T13:32:13.413 回答