访问 .zip 存档中的文件而不提取它们 [英] Accessing files inside a .zip archive without extracting them
问题描述
我正在尝试读取 .zip 中的 NIFTI 文件,而无需将该目录解压缩到根目录.更具体地说,我正在使用 ADNI 数据库,并且文件按主题 ID 存储在单独的 .zip 文件中.在 .zip 文件中包含与该主题相关的所有数据,我想从 .zip 中提取 NIFTI 文件 (.nii.gz) 而不提取文件.
目前我有以下代码片段
def openNIFTI(文件名):返回 nib.load(filename).get_data()zip_filename = 文件路径 + str(subject_id) + '_3T_Structural_unproc.zip'文件名 = str(subject_id) + '/unprocessed/3T/T1w_MPR1/' + str(subject_id) + '_3T_T1w_MPR1.nii.gz'文件 = zf.extract(文件名)数据 = openNIFTI(文件)
filepath 是 .zip 文件集合的路径.filename 是 .zip 文件中我要提取的 NIFTI 文件的路径.
<小时>(编辑)
错误似乎来自 nibabel 加载函数.然后函数检查
如果不是 op.exists(filename):
在独立测试 os.path.exists(filename) 函数后,我发现了这一点.
os.path.exists(r'C:/Users/eee/workspace_python/Image Reconstruction/data/ADNI/MRI data/100206_3T_Structural_unproc.zip/100206/unprocessed/3T/T1w_MPR1/100206_3T.zi.t1w')
<块引用>
错误
但是,此路径是从我尝试打开的文件中复制/粘贴的.在我看来,错误是由于文件路径中的 .zip 而出现的,因为
os.path.exists(r'C:/Users/eee/workspace_python/Image Reconstruction/data/ADNI/MRI data/100206_3T_Structural_unproc.zip')
<块引用>
正确
还有其他方法吗?
请参阅接受的答案 python:从 zip 打开文件而不临时解压缩它 这显示了如何在不解压缩文件的情况下从 zipfile 中读取数据,(当然你需要足够的 RAM 来处理文件内容).
I am attempting to read a NIFTI file inside a .zip without having to extract the directory to the root directory. More specifically, I am working with the ADNI database and the files are stored by subjectID in individual .zip files. Inside the .zip file there is all the data pertaining to that subject, I would like to extract the NIFTI file (.nii.gz) from within the .zip without extracting the files.
Currently I have the following code snippet
def openNIFTI(filename):
return nib.load(filename).get_data()
zip_filename = filepath + str(subject_id) + '_3T_Structural_unproc.zip'
filename = str(subject_id) + '/unprocessed/3T/T1w_MPR1/' + str(subject_id) + '_3T_T1w_MPR1.nii.gz'
file = zf.extract(filename)
data = openNIFTI(file)
The filepath is the path to the collection of .zip files. filename is the path inside the .zip file to the NIFTI file I want to extract.
(Edit)
It seem the error is coming from the nibabel load function. Then function checks
if not op.exists(filename):
Upon testing the os.path.exists(filename) function independently I found that.
os.path.exists(r'C:/Users/eee/workspace_python/Image Reconstruction/data/ADNI/MRI data/100206_3T_Structural_unproc.zip/100206/unprocessed/3T/T1w_MPR1/100206_3T_T1w_MPR1.nii.gz')
False
However, this path is copy/pasted from the file I am attempting to open. It seems to me the error appears due to the .zip in the file path because
os.path.exists(r'C:/Users/eee/workspace_python/Image Reconstruction/data/ADNI/MRI data/100206_3T_Structural_unproc.zip')
True
Is there another way of doing this?
Please see the accepted answer to python: Open file from zip without temporary extracting it this shows how to read the data from within a zipfile without extracting the file, (of course you need enough RAM to handle the file contents).
这篇关于访问 .zip 存档中的文件而不提取它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!