-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrmexcept.py
More file actions
executable file
·52 lines (43 loc) · 1.29 KB
/
rmexcept.py
File metadata and controls
executable file
·52 lines (43 loc) · 1.29 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
#!/usr/bin/env python3
import os
import sys
import shutil
from pathlib import Path
def main():
# Parse arguments - files to keep
keep_files = set(sys.argv[1:])
if not keep_files:
print("Usage: rmexcept file1 file2 file3 ...")
print("Deletes everything in current directory except specified files")
sys.exit(1)
cwd = Path.cwd()
to_delete = []
# Find everything to delete
for item in cwd.iterdir():
if item.name not in keep_files:
to_delete.append(item)
if not to_delete:
print("Nothing to delete (all files/dirs are protected)")
return
# Show what will be deleted
print(f"Will delete {len(to_delete)} items:")
for item in to_delete:
item_type = "DIR" if item.is_dir() else "FILE"
print(f" [{item_type}] {item.name}")
# Confirm
response = input("\nProceed? (y/n): ").strip().lower()
if response != 'y':
print("Cancelled")
return
# Delete
for item in to_delete:
try:
if item.is_dir():
shutil.rmtree(item)
else:
item.unlink()
print(f"Deleted: {item.name}")
except Exception as e:
print(f"Error deleting {item.name}: {e}")
if __name__ == '__main__':
main()