I want to create a tar archive with a hierarchical directory structure from Python, using strings for the contents of the files. I’ve read this question , which shows a way of adding strings as files, but not as directories. How can I add directories on the fly to a tar archive without actually making them?
Something like:
archive.tgz:
file1.txt
file2.txt
dir1/
file3.txt
dir2/
file4.txt
Extending the example given in the question linked, you can do it as follows:
Be careful with
modeattribute since default value might not include execute permissions for the owner of the directory which is needed to change to it and get its contents.