Kokomelom
Kokomelom

Reputation: 353

Get tar file buffer without write to file with Python

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?

Upvotes: 1

Views: 161

Answers (1)

Majid
Majid

Reputation: 638

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

Upvotes: 0

Related Questions