2017-10-27 17 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

をご提案いただきありがとうございますが、それは本当に私のためのソリューションではありません! 1つのコマンドでデータを分析した後、解凍されたフォルダ全体を削除することもできます。 –

関連する問題