-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
142 lines (111 loc) · 6.08 KB
/
Copy pathmain.py
File metadata and controls
142 lines (111 loc) · 6.08 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Import Final for marking constants
from typing import Final
from calculate_total_size import size_of_suspicious_names
# Import functions from modules
from file_reader import read_suspicious_criteria
from scanner import scan_directory, display_scan_results
from suspicious import detect_suspicious_files
from safe_files import mark_file_safe
from logger import log_suspicious_files
from statistics import display_statistics
from delete_suspicious import delete_suspicious_files
from filter_by_extension import file_extension_suspicious
from count_suspicous_level import files_by_suspicious_level
# Define constant file paths for suspicious extensions and names
SUSPICIOUS_EXTENSIONS_FILE: Final = "suspicious_file_types.txt"
SUSPICIOUS_NAMES_FILE: Final = "suspicious_file_names.txt"
def main():
"""
Main function displaying a menu that allows the user to choose one of the following options:
1. Scan Folder
2. Identify suspicious files and log them
3. Mark File as Safe
4. Display Statistics
5. Delete Suspicious Files
6. Display amount for each suspicious level
7. Filter suspicions by extension
8. Display total size of files that have suspicious name
9. Exit
The user selects an option by entering a number, and the related action is executed.
If an invalid option is selected,the program asks again the user to enter a valid number between 1-6.
If an error occurs message is displayed.
"""
try:
print("========Welcome to the File Scanning System😊========")
# Load (Read) the suspicious file extensions and names from the specified files
suspicious_extensions = read_suspicious_criteria(SUSPICIOUS_EXTENSIONS_FILE)
suspicious_names = read_suspicious_criteria(SUSPICIOUS_NAMES_FILE)
# Initialize an empty list to keep track of safe files
safe_files = []
# Automatically run Option 1 (Scan Folder) on first run
folder_path = input("\nEnter the folder path to scan on startup: ").strip()
scan_results = scan_directory(folder_path) # Scan the folder
display_scan_results(scan_results) # Display scan results for the files in 'scan_results'.
# Menu loop to allow the user to choose options
while True:
# Detect suspicious files
suspicious_files = detect_suspicious_files(scan_results, suspicious_extensions, suspicious_names,
safe_files)
# Display Menu
print("\nMenu Options:")
print(" 1. Scan a folder 📂🔍")
print(" 2. Identify suspicious files and log them 📜🚨")
print(" 3. Mark a file as safe ✅🛡️")
print(" 4. Display scan statistics 📊")
print(" 5. Delete suspicious files 🗑️🚨")
print(" 6. Display amount for each suspicious level 📊🚨")
print(" 7. Filter suspicions by extension 🔍")
print(" 8. Display total size of files that have suspicious name 📂🚨")
print(" 9. Exit 🚪")
# Get the user's choice
choice = input("\nPlease choose an option (1-9): ").strip()
match choice:
# Option 1: Scan folder and display results (File Name, Extension, Size, Full Path)
case "1":
folder_path = input("\nEnter the folder path to scan on startup: ").strip()
scan_results = scan_directory(folder_path) # Scan the folder
safe_files = []
display_scan_results(scan_results) # Display scan results for the files in 'scan_results'.
# Option 2: Log suspicious files
case "2":
log_suspicious_files(suspicious_files)
# Option 3: Mark a file as safe
case "3":
file_name = input("Enter name of the file to mark as safe (with extension): ").strip()
# Loop to check if file to mark as safe exist in the last scan, mark it.
for file in scan_results:
if '.'.join([file["name"], file["extension"]]) == file_name:
safe_files = mark_file_safe(file_name, safe_files)
break
else: # file to mark not found in the last scan
print(f"⚠️File '{file_name}' not exist in directory '{folder_path}' to mark safe")
# Option 4: Display statistics for last scan
case "4":
display_statistics(scan_results, suspicious_files)
# Option 5: Delete Suspicious Files if any are found, after user confirmation
case "5":
delete_suspicious_files(suspicious_files)
# Option 6: Display amount for each suspicious level
case "6":
files_by_suspicious_level(suspicious_files)
# Option 7: Filter suspicions by extension
case "7":
ext_to_find = input("Enter extension that in the last scan and considered suspicious(without dot): ")
file_extension_suspicious(suspicious_files, ext_to_find)
# Option 8: Display total size of files that have suspicious name
case "8":
total_size = size_of_suspicious_names(suspicious_files, suspicious_names)
print(f"The total size of files with suspicious names is: {total_size} MB")
# Option 9: Exit the system
case "9":
print("Exiting the system. Goodbye👋")
print("©2025 'Bshara'. All rights reserved.")
break # End the loop to end the program
# Handle invalid Option
case _:
print("Invalid option. Please choose an option between 1 and 9.")
# Handle general exceptions
except Exception as e:
print(f"❌An error occurred: {e}")
if __name__ == "__main__":
main()