2017-10-27 5 views
0

압축 해제하지 않고 zip 파일에서 파일을 여는 방법을 알고 있습니다. 다음과 같이 수행 할 수 있습니다 :압축 해제하지 않고 pycdf로 zip 파일 객체를 읽습니다.

import zipfile 
archive = zipfile.ZipFile(path_to_zipfile, 'r') 
my_zipfile = archive.open('file_01.cdf') # "file_01.cdf" is contained in the zipfile 

내 문제가 : zip 파일에 포함 된 파일 "file_01.cdf는"CDF 형식입니다. 다음과 같이 일반적으로, CDF 형식의 파일을 읽을 수 있습니다 :

from spacepy import pycdf 

cdf_file = pycdf.CDF(path_to_file) 
data = cdf_file.copy() 

문제는 : 기능 pycdf.CDF 문자열 형식의 인수를 받아들입니다. 그러나, 나는 pycdf.CDF 함수의 인수로 zipfile 객체를 전달하려고합니다. (Zip 파일의 압축을 풀지 않기 때문에 파일로 저장할 경로가 없습니다.) 이 문제를 극복 할 수있는 아이디어가 있습니까? 고마워요!

답변

0

나는 당신이 특별히 압축을 풀지 않고 그것을 수행하는 것에 대해 알고 있지만 그것은 불가능하다고 생각합니다 (누군가 틀렸다면 누군가는 나를 교정 할 것입니다.).

import zipfile 
import os 
import tempfile 
import shutil 

path = r'C:\Users\XXX\Desktop\test.zip' # my zip file 
archive = zipfile.ZipFile(path, 'r') 

tmpdir = tempfile.mkdtemp(dir=os.getcwd()) # create a temp folder in the working directory 
archive.extract('test.txt', path=tmpdir) # extract your file to the temp folder 
path_to_my_file = os.path.join(tmpdir, 'test.txt') # this is the path to the extracted file 
# ------- 
# do stuff with your file path 
# ------- 
print(path_to_my_file) 
shutil.rmtree(tmpdir) # delete the temp folder 
+0

이 당신의 제안을 주셔서 감사합니다,하지만 나를 위해 정말 해결책이 아니다 :

에게 당신이 뭔가를 할 수 있습니다! 하나의 명령으로 데이터를 분석 한 후 압축되지 않은 폴더 전체를 삭제할 수도 있습니다. –