I am using Python 3 with the tarfile
module to compress some folders (with subfolders). What I need to do: to set a couple of subfolders to be excluded from the final tar file.
For example, say my folders looked like:
dir/
├── subdirA
│ ├── subsubdirA1
│ │ └── fileA11.txt
│ │ └── fileA12.txt
│ ├── subsubdirA2
│ │ └── fileA21.txt
│ │ └── fileA22.txt
│ └── fileA.txt
├── subdirB
│ ├── subsubdirB1
│ │ └── fileB11.txt
│ │ └── fileA12.txt
│ ├── subsubdirB2
│ │ └── fileB21.txt
│ │ └── fileB22.txt
│ └── fileB.txt
└── main.txt
Now, I say I wanted to include everything in dir/
except the contents of subsubdirA2
and of subsubdirB2
. Based on this answer, I have tried:
EXCLUDE_FILES = ['/subdirA/subsubdirA2', '/subdirB/subsubdirB2']
mytarfile.add(..., filter=lambda x: None if x.name in EXCLUDE_FILES else x)
Or:
EXCLUDE_FILES = ['/subdirA/subsubdirA2/*', '/subdirB/subsubdirB2/*']
mytarfile.add(..., filter=lambda x: None if x.name in EXCLUDE_FILES else x)
Or:
EXCLUDE_FILES = ['/subdirA/subsubdirA2/*.*', '/subdirB/subsubdirB2/*.*']
mytarfile.add(..., filter=lambda x: None if x.name in EXCLUDE_FILES else x)
I also tried variants of the three options above where the subfolder paths started without /
or with dir
or with /dir
. None worked - all the time, everything within dir
was included.
How could I correctly exclude specific subfolders from a tar file I want to generate? If a different module/library is required instead of tarfile
, that is fine.