Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add empty directory to tarfile

Tags:

python

tar

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
like image 338
mirk Avatar asked Oct 11 '12 16:10

mirk


1 Answers

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)
like image 69
Nathan Villaescusa Avatar answered Sep 28 '22 05:09

Nathan Villaescusa