简体   繁体   中英

Python: how to pass a file from a zip to a function that reads data from that file

I have a zip-file that contains .nrrd type files. The pynrrd lib comes with a read function. How can I pull the .nrrd file from the zip and pass it to the nrrd.read() function?

I tried following, but that gives the following error at the nrrd.read() line:

TypeError was unhandled by user code, file() argument 1 must be encoded string without NULL bytes, not str

in_dir = r'D:\Temp\Slikvideo\JPEG\SV_4_1_mask'
zip_file = 'Annotated.mitk'

zf = zipfile.ZipFile(in_dir + '\\' + zip_file)

f_name = 'datafile.nrrd'    # .nrrd file in zip

file_nrrd = zf.read(f_name)    # pull the file from the zip

img_nrrd, options = nrrd.read(file_nrrd)    # read the .nrrd image data from the file

I could write the file pulled from the .zip to disk, and then read it from disk with nrrd.read() but I am sure there is a better way.

I think that your is a good way...

Here there is a similar question:

Similar question

Plus answer: I think that the problem maybe is that when you use zipfile.ZipFile you not set the attribute: Try using:

zipfile.ZipFile (path,"r")

以下作品:

file_nrrd = zf.extract(f_name)    # extract the file from the zip

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM