2012-01-27 4 views
3

zipfile 모듈을 사용하여 보관 된 파일을 추출하는 스크립트를 만들었지 만이 방법으로 txt 파일 이외의 모든 파일이 손상됩니다. 여기Windows에서 Python으로 WinRar를 호출하려면 어떻게해야합니까? STILL Problematic

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를 명령은 문제의 프로그램에서 사용할 수 없게 렌더링 것 같다 - 내가는 ArcGIS에서 사용 shape 파일을 압축 해제하려고 ...

     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

답변 해 주셔서 감사합니다. 트레이스 백 (가장 최근의 마지막 콜) : 파일 "C : \ Python26 \ ArcGIS10.0 \ lib \ threading.py", 줄 532, __bootstrap_inner에 self.run() 에 오류가 발생했습니다. 실행 파일에서 "C : \ Python26 \ ArcGIS10.0 \ lib \ threading.py"파일을 실행하십시오. self .__ target (* self .__ args, ** self .__ kwargs) 파일 "build \ bdist.win32 \ egg \ envoy \ core.py ", 목표 40 줄 4035, bufize = 0, 파일"C : \ Python26 \ ArcGIS10.0 \ lib \ subprocess.py ", 줄 633, __init__에서 errread, errwrite) 파일"C : \ Python26 \ ArcGIS10.0 \ lib \ subprocess.py ", 줄 842, _execute_child ... –

+0

더 구체적으로 파일"M : \ SVN_EReportingZones \ eReportingZones \ data \ scripts \ file_unzipper_alpha999.py ", 줄 101, 압축 풀기"build \ bdist.win32 \ egg \ envoy \ core.py"파일, 줄 167, 실행 out, err = cmd에서 r = envoy.run ('unrar e {0}'. 형식 (압축 풀기)) 파일. 실행 파일에서 "build \ bdist.win32 \ egg \ envoy \ core.py"파일을 실행하십시오. self.returncode = self.process.returncode AttributeError : 'NoneType'객체에 속성이 없습니다 ' returncode ' –

+0

@AlexOulton 나는 단지 python27에 특사를 사용했습니다. 내 생각에 사절은 2.7으로 추가 된 기능 만 사용하려고 시도 할 것이므로 하위 버전 전용 버전을 사용해보십시오. –

3

모드로 파일을 여는 중입니다. 시도 :

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

는 런타임 라이브러리는 어떤 줄 바꿈 변환을 수행하려고하지 않는 모드로 파일을 엽니 다.

+0

감사합니다를! 그것은 지금 완벽하게 작동합니다! –

관련 문제