This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Tarfile to stdout documentation example
Type: Stage:
Components: Documentation Versions: Python 3.9, Python 3.8, Python 3.7, Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, ilaughlin
Priority: normal Keywords:

Created on 2020-11-23 12:10 by ilaughlin, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg381665 - (view) Author: Ian Laughlin (ilaughlin) Date: 2020-11-23 12:10
Recommend adding example to tarfile documentation to provide example of writing a tarfile to stdout.


example:

files = [(file_1, filename_1), (file_2, filename_2)]


with tarfile.open(fileobj=sys.stdout.buffer, mode = 'w|gz') as tar:
    for file, filename in files:
        file_obj = io.BytesIO() #starts a BytesIO object
        file_obj.write(file.encode('utf-8')) #writes the file to the BytesIO object
        info = tarfile.TarInfo(filename) #creates the TarInfo
        file_obj.seek(0) #goes to the beginning of the BytesIO object else it won't write
        info.size = len(file) #sets the length of the file
        tar.addfile(info, fileobj=file_obj) #writes the tar to stdout.
History
Date User Action Args
2022-04-11 14:59:38adminsetgithub: 86608
2020-11-23 12:10:03ilaughlincreate