-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_tar_files.py
54 lines (43 loc) · 1.71 KB
/
extract_tar_files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
import tarfile
def extract_tar_gz(file_path, extract_path='.'):
"""
Extracts a .tar.gz file to the specified directory.
:param file_path: Path to the .tar.gz file
:param extract_path: Directory where the files will be extracted (default is
current directory)
"""
try:
with tarfile.open(file_path, 'r:gz') as tar:
tar.extractall(path=extract_path)
print(f"Extracted {file_path} to {extract_path}")
except Exception as e:
print(f"An error occurred: {e}")
def extract_and_delete_tar_gz(file_path, extract_path='.'):
"""
Extracts a .tar.gz file and then deletes it.
:param file_path: Path to the .tar.gz file
:param extract_path: Directory where the files will be extracted (default is
current directory)
"""
extract_tar_gz(file_path, extract_path)
try:
os.remove(file_path)
print(f"Deleted {file_path}")
except Exception as e:
print(f"An error occurred while deleting {file_path}: {e}")
def extract_all_tar_gz_in_directory(directory_path, extract_path='.'):
"""
Iterates over all .tar.gz files in a directory, extracts them, and then
deletes them.
:param directory_path: Path to the directory containing .tar.gz files
:param extract_path: Directory where the files will be extracted (default is
current directory)
"""
for file_name in os.listdir(directory_path):
if file_name.endswith('.tar.gz'):
file_path = os.path.join(directory_path, file_name)
extract_and_delete_tar_gz(file_path, extract_path)
# Example usage:
source_dir = os.path.join(os.getcwd(), 'data', 'EEG_preprocessed')
extract_all_tar_gz_in_directory(source_dir, source_dir)