Skip to content

Instantly share code, notes, and snippets.

@lapis-zero09
Created August 14, 2018 06:28
Show Gist options
  • Save lapis-zero09/c9b24c4fbe182905776f43a346d53892 to your computer and use it in GitHub Desktop.
Save lapis-zero09/c9b24c4fbe182905776f43a346d53892 to your computer and use it in GitHub Desktop.
import os
import tarfile
def recursive_files(dir_name='.', ignore=None):
for dir_name,subdirs,files in os.walk(dir_name):
if ignore and os.path.basename(dir_name) in ignore:
continue
for file_name in files:
if ignore and file_name in ignore:
continue
yield os.path.join(dir_name, file_name)
def make_tar_file(dir_name='.', tar_file_name='tarfile.tar', ignore=None):
tar = tarfile.open(tar_file_name, 'w')
for file_name in recursive_files(dir_name, ignore):
tar.add(file_name)
tar.close()
dir_name = '.'
tar_file_name = 'archive.tar'
ignore = {'.ipynb_checkpoints', '__pycache__', tar_file_name}
make_tar_file(dir_name, tar_file_name, ignore)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment