Get tar file buffer without write to file with Python

120 views Asked by At

I know how to tar file using 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.

Hiw can I do that please?

1

There are 1 answers

1
Majid On

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()