How do I add an empty directory to a tarfile in python, without creating it on disk first?
Creating an empty directory in my local filesystem, and adding this to the tar-file is easy enough, but creates unnecessary overhead.
Creating one directly in the tar-file, however seems non-trivial. My attempts looks like:
import tarfile
with tarfile.open("test.tbz2",mode='w:bz2') as t:
t.add("conf_dir") # does not work
t.add(tarfile.TarInfo("conf_dir")) # does not work
Use addfile() and change the TarInfo.type to tarfile.DIRTYPE
import tarfile
with tarfile.open("test.tbz2",mode='w:bz2') as f:
t = tarfile.TarInfo('mydir')
t.type = tarfile.DIRTYPE
f.addfile(t)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With