-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfileRemoveByExtension.py
48 lines (35 loc) · 1.13 KB
/
fileRemoveByExtension.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
import sys
from pathlib import Path
def delete_files_by_extension(extensions, path="."):
"""Delete files with given extensions in the specified path."""
deleted_file_count = 0
log = ""
p = Path(path)
for ext in extensions:
file_list = list(p.rglob(f"*.{ext}"))
if not file_list:
print(f"NO {ext.upper()} FILES TO REMOVE.")
continue
print(f"Removing: {ext.upper()}")
for file in file_list:
if file.is_file():
file.unlink()
deleted_file_count += 1
print(file)
log += f"{file}\n"
print("\n", end="")
log += f"{deleted_file_count} FILES DELETED.\n"
return log, deleted_file_count
def write_log(log, filename="delete_log.log"):
"""Write the log to a file."""
if log:
with open(filename, "a") as log_file:
log_file.write(log)
def main():
if len(sys.argv) > 1:
log, deleted_file_count = delete_files_by_extension(sys.argv[1:])
write_log(log)
else:
sys.exit("NO ARGUMENT PASSED.")
if __name__ == "__main__":
main()