2011-08-10 3 views
2
내가 파이썬 내가 현재 사용하는 코드의 ZipFile에 모듈을 실험하고있어

은 이것이다 :파이썬 ZipFile를 디렉토리에 복사됩니다 너무

z = zipfile.ZipFile("jar/algorithms.jar", "w") 
z.write('directory/QuickSort.class') 

문제는 내 파일이 다음과 같은 항아리에 추가한다는 것입니다 :

algorithms.jar> 디렉토리는 내가 원하는 QuickSort.class

입니다> : algorithms.jar> QuickSort.class

H 나 한테 이럴 수있어? -

답변

4

당신은 arcname 매개 변수를 사용할 수 있습니다 http://docs.python.org/library/zipfile.html#zipfile.ZipFile.write

z.write("directory/QuickSort.class","QuickSort.class") 
+0

잘 작동하는 것 같습니다. 여기에도 작은 문제가 있습니다. QInputDialog를 사용하여 .class 파일의 경로를 얻으려고합니다. 이 경로가 'H :/Documenten van Walter/Documenten/Python/PyComplexity/src/jar/someclass 인 경우이 경로를 사용하십시오. class ' 어떻게 아크 네임을 얻을 수 있습니까? – tgoossens

+0

http://docs.python.org/library/os.path.html#os.path.basename – Ofir

0

당신은 당신이 파일은 다음과 write에 두 번째 매개 변수로 아카이브에 부여 할 이름을 제공 할 수 있습니다 참조 :

In [39]: z = zipfile.ZipFile("jar/algorithms.jar", "w") 

In [40]: z.printdir() 
File Name            Modified    Size 

In [41]: z.write("directory/QuickSort.class", "QuickSort.class") 

In [42]: z.printdir() 
File Name            Modified    Size 
QuickSort.class        2011-08-10 15:26:47   0 

In [43]: z.close() 

zipfile documentation도 참조하십시오.

+0

잘 작동하는 것 같습니다. 그래도 여기에 작은 문제가 있습니다. QInputDialog를 사용하여 .class 파일의 경로를 얻으려는 경우 Path'H :/Documenten 밴 월터/Documenten/파이썬/Pycomplexity/src/jar/someclass.class '어떻게 할 수 있습니다 나는 그때 아크 네임을 얻는다. – tgoossens

+0

path가 'H :/Documentum/Wallet/Documenten/Python/PyComplexity/src/jar/someclass.class'로 설정되어 있다면 다음과 같이 os.path의 메소드를 사용할 수 있습니다 :'arcname = os.path.basename (os. path.abspath (경로))' – sandinmyjoints