Refactor zipremove.py to process all ZIP files in a directory
This commit is contained in:
parent
808370ef1f
commit
6b6c06d0e5
1 changed files with 11 additions and 8 deletions
17
zipremove.py
17
zipremove.py
|
@ -2,27 +2,30 @@ import zipfile
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
def filter_and_repack_zip(zip_path):
|
def filter_and_repack_zip(zip_path):
|
||||||
# Temporary directory to hold the contents we want to keep
|
|
||||||
temp_dir = tempfile.TemporaryDirectory()
|
temp_dir = tempfile.TemporaryDirectory()
|
||||||
new_zip_path = os.path.join(temp_dir.name, 'new_archive.zip')
|
new_zip_path = os.path.join(temp_dir.name, 'new_archive.zip')
|
||||||
|
|
||||||
with zipfile.ZipFile(zip_path, 'r') as original_zip:
|
with zipfile.ZipFile(zip_path, 'r') as original_zip:
|
||||||
with zipfile.ZipFile(new_zip_path, 'w') as new_zip:
|
with zipfile.ZipFile(new_zip_path, 'w') as new_zip:
|
||||||
for file in original_zip.namelist():
|
for file in original_zip.namelist():
|
||||||
# Conditions for keeping the file
|
|
||||||
if "(USA" in file or "(USA, Europe)" in file:
|
if "(USA" in file or "(USA, Europe)" in file:
|
||||||
new_zip.writestr(file, original_zip.read(file))
|
new_zip.writestr(file, original_zip.read(file))
|
||||||
|
|
||||||
# Replace the original zip file with the new one
|
|
||||||
os.replace(new_zip_path, zip_path)
|
os.replace(new_zip_path, zip_path)
|
||||||
|
|
||||||
|
def process_directory(directory_path):
|
||||||
|
for zip_file in Path(directory_path).glob('*.zip'):
|
||||||
|
print(f"Processing {zip_file}")
|
||||||
|
filter_and_repack_zip(zip_file)
|
||||||
|
print("All ZIP files processed.")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
if len(sys.argv) != 2:
|
if len(sys.argv) != 2:
|
||||||
print("Usage: python script.py <zip_file_path>")
|
print("Usage: python script.py <directory_path>")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
zip_file_path = sys.argv[1]
|
directory_path = sys.argv[1]
|
||||||
filter_and_repack_zip(zip_file_path)
|
process_directory(directory_path)
|
||||||
print(f"Processed {zip_file_path}")
|
|
||||||
|
|
Loading…
Reference in a new issue