简体   繁体   English

获取 tar 文件缓冲区而不使用 Python 写入文件

[英]Get tar file buffer without write to file with Python

I know how to tar file using Python我知道如何使用 Python 压缩文件

import os
import tarfile
with tarfile.open('res.tar.gz','w:xz' )as tar :
    tar.add('Pic.jpeg')

But I want to do that without create any tar.gz file, only get the results buffer.但我想在不创建任何 tar.gz 文件的情况下这样做,只获取结果缓冲区。

Hiw can I do that please?嗨,我可以这样做吗?

You could use this code to access result buffer您可以使用此代码访问结果缓冲区

from io import BytesIO
import os
import tarfile

buf = BytesIO()
with tarfile.open('/tmp/res.tar.gz', 'w:gz', fileobj=buf) as f:
    f.add("Pic.jpeg")
    data = buf.getvalue()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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