Skip to content

Instantly share code, notes, and snippets.

@ripiuk
Created December 20, 2017 16:02
Show Gist options
  • Save ripiuk/556a35f46c5889781c5619f632c4501b to your computer and use it in GitHub Desktop.
Save ripiuk/556a35f46c5889781c5619f632c4501b to your computer and use it in GitHub Desktop.
Extract all tars in the directory
import os
import tarfile
PATH_TO_DIR_WITH_TARS = '/home/username/sub_folder/'
files = [file for file in os.listdir(PATH_TO_DIR_WITH_TARS) if
os.path.isfile(os.path.join(PATH_TO_DIR_WITH_TARS, file))]
for file in files:
file_name, file_extension = os.path.splitext(file)
if file_extension == '.tar':
with tarfile.open(os.path.join(PATH_TO_DIR_WITH_TARS, file)) as tar:
tar.extractall(path=PATH_TO_DIR_WITH_TARS) # extract files to the same directory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment