簡體   English   中英

Zip文件不可尋找

[英]Zip file not seekable

我正在嘗試對壓縮文件進行一些文件操作。 我有點困惑為什么文件像對象.open返回一個不可尋找的文件。 任何人都可以對此有所了解嗎?

這是我正在使用的代碼。 我怎樣才能使file_like可以搜索?

zipped_archive = ZipFile(filepath, mode='r')
file_like = zipped_archive.open(file_name, mode='r')
file_like.seekable() # returns False

ZipFile.open方法返回一個ZipExtFile對象,該對象實現搜索。 io.IOBase可搜索的默認值為 False

我不確定為什么搜索從未實現但我猜測在壓縮文件中按字節搜索可能由於某種原因很難。

在Python 3.7版( 問題 )中修復

一個非常有用和簡單的工作幫助我添加可搜索到我使用的文件:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM