I'm trying to do some file manipulation on a zipped file. I'm a little confused on why the file like object that .open is returning a file like that's not seekable. Can any one shed some light on this?
This is the code I'm using. How can I make file_like's seekable?
zipped_archive = ZipFile(filepath, mode='r')
file_like = zipped_archive.open(file_name, mode='r')
file_like.seekable() # returns False
The ZipFile.open
method returns a ZipExtFile
object which does not implement seeking. The default value for seekable in io.IOBase
is False
.
I'm not sure why seeking was never implemented but I'd guess seeking by byte in a compressed file might be difficult for some reason.
在Python 3.7版( 问题 )中修复
A very helpful and simple work around that helped me is to add seekable to the file I used:
def add_seekable_to_file(f):
"""
If file f does not has seekable function -
add seekable function that will always return true
Args:
f: the file
Returns: the file f with seekable function
"""
if not hasattr(f, "seekable"):
# AFAICT all the filetypes that STF wraps can seek
f.seekable = lambda: True
add_seekable_to_file(datazip.fp)
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.