forked from rcap107/retrieve-merge-predict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchive_cand.py
59 lines (47 loc) · 1.7 KB
/
archive_cand.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
55
56
57
58
59
import argparse
import os
import zipfile
from datetime import datetime
def create_archive(file_list_path, output_archive):
# Check if the file list exists
if not os.path.exists(file_list_path):
print(f"Error: File list '{file_list_path}' not found.")
return
# Create a ZipFile object
with zipfile.ZipFile(output_archive, "w", zipfile.ZIP_DEFLATED) as zipf:
# Read the file list
with open(file_list_path, "r") as file_list:
for line in file_list:
file_path = line.strip()
# Check if the file exists
if os.path.exists(file_path):
# Add file to the archive
zipf.write(file_path, os.path.basename(file_path))
print(f"Added: {file_path}")
else:
print(f"Warning: File not found - {file_path}")
print(f"\nArchive created successfully: {output_archive}")
def main():
# Set up argument parser
parser = argparse.ArgumentParser(
description="Create an archive from a list of files."
)
parser.add_argument(
"file_list",
help="Path to the text file containing the list of files to archive",
)
parser.add_argument(
"-o",
"--output",
help="Name of the output archive (default: archive_YYYYMMDD_HHMMSS.zip)",
)
# Parse arguments
args = parser.parse_args()
# Generate default output name if not provided
if not args.output:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
args.output = f"archive_{timestamp}.zip"
# Create the archive
create_archive(args.file_list, args.output)
if __name__ == "__main__":
main()