簡體   English   中英

用python修改gzip壓縮文件

[英]modify gzipped file with python

我正在嘗試修改壓縮文件。 這是我的代碼:

with tempfile.TemporaryFile() as tmp:
    with gzip.open(fname, 'rb') as f:
        shutil.copyfileobj(f, tmp)
    # do smth here later
    with gzip.open(fname, 'wb') as f:
        shutil.copyfileobj(tmp, f)

我刪除了所有修改,只保留了讀寫功能。 在輸出時,我得到了空的gzip壓縮文件。 這怎么了 (Python 2.7.6,Linux)

復制后,您需要指向臨時文件的開頭:

with tempfile.TemporaryFile() as tmp:
    with gzip.open(fname, 'rb') as f:
        shutil.copyfileobj(f, tmp)
        tmp.seek(0)

暫無
暫無

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

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