[New-bugs-announce] [issue42442] Tarfile to stdout documentation example

Ian Laughlin report at bugs.python.org
Mon Nov 23 07:10:03 EST 2020


New submission from Ian Laughlin <ilaughlin at novetta.com>:

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.

----------
assignee: docs at python
components: Documentation
messages: 381665
nosy: docs at python, ilaughlin
priority: normal
severity: normal
status: open
title: Tarfile to stdout documentation example
versions: Python 3.6, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42442>
_______________________________________


More information about the New-bugs-announce mailing list