2012-01-27 94 views
3

使用zipfile模块我创建了一个脚本来提取我的归档文件,但该方法正在破坏除txt文件以外的所有内容。如何在Windows上使用Python调用WinRar?仍然存在问题

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 

在这里,我有一个问题 - 通过读取和写入的数据压缩,zip文件命令似乎使其不可通过相关的方案 - 我想解压缩shape文件在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的方向来完成解压缩过程吗?

回答

0

要回答你的问题的第二部分,我建议envoy library。与特使使用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) 
+0

感谢您的回答。不幸的是,安装envoy代码吐出这个错误:Traceback(最近调用最后一个): 文件“C:\ Python26 \ ArcGIS10.0 \ lib \ threading.py”,第532行,在__bootstrap_inner中 self.run() 文件“C:\ Python26 \ ArcGIS10.0 \ lib \ threading.py”,行484,在运行 self .__ target(* self .__ args,** self .__ kwargs) File“build \ bdist.win32 \ egg \文件“C:\ Python26 \ ArcGIS10.0 \ lib \ subprocess.py”,第633行,在__init__中 errread,errwrite) 文件“C:envoy \ core.py”,第40行,目标为 bufsize = 0, 文件“C :\ Python26 \ ArcGIS10.0 \ lib \ subprocess.py“,行842,在_execute_child中...... – 2012-02-02 14:46:22

+0

更具体的文件”M:\ SVN_EReportingZones \ eReportingZones \ data \ scripts \ file_unzipper_alpha999.py“,行101,解压缩后 r = envoy.run('unrar e {0}'.format(unpack)) 运行中的文件“build \ bdist.win32 \ egg \ envoy \ core.py”,第167行,运行 out,err = cmd。运行(数据,超时) 文件“build \ bdist.win32 \ egg \ envoy \ core.py”,行52,运行 self.returncode = self.process.returncode AttributeError:'NoneType'对象没有属性' returncode' – 2012-02-02 14:50:41

+0

@AlexOulton我只用了python27的envoy。我的猜测是特使试图使用仅添加了2.7的功能,因此请尝试仅用于子流程的版本。 – 2012-02-02 15:13:39

3

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

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

b打开文件的二进制模式,在运行时库不要试图做任何换行转换。

+0

三江源!它现在完美运行! – 2012-01-27 10:27:42

相关问题