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)]
withtarfile.open(fileobj=sys.stdout.buffer, mode='w|gz') astar:
forfile, filenameinfiles:
file_obj=io.BytesIO() #starts a BytesIO objectfile_obj.write(file.encode('utf-8')) #writes the file to the BytesIO objectinfo=tarfile.TarInfo(filename) #creates the TarInfofile_obj.seek(0) #goes to the beginning of the BytesIO object else it won't writeinfo.size=len(file) #sets the length of the filetar.addfile(info, fileobj=file_obj) #writes the tar to stdout.
Activity
ilaughlin commented on Nov 23, 2020
Recommend adding example to tarfile documentation to provide example of writing a tarfile to stdout.
example:
gh-86608: Improve and restructure tarfile examples (#121771)
pythongh-86608: Improve and restructure tarfile examples (pythonGH-12…
pythongh-86608: Improve and restructure tarfile examples (pythonGH-12…
pythongh-86608: Improve and restructure tarfile examples (python#121771)
[3.14] gh-86608: Improve and restructure tarfile examples (GH-121771) (…
[3.13] gh-86608: Improve and restructure tarfile examples (GH-121771) (…
pythongh-86608: Improve and restructure tarfile examples (python#121771)
pythongh-86608: Improve and restructure tarfile examples (python#121771)
[3.14] pythongh-86608: Improve and restructure tarfile examples (pyth…