-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_json_files.py
More file actions
43 lines (33 loc) · 1.28 KB
/
Copy pathdelete_json_files.py
File metadata and controls
43 lines (33 loc) · 1.28 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
import os
import argparse
import glob
def main():
parser = argparse.ArgumentParser(description="Recursively delete all JSON files in a directory.")
parser.add_argument("dir", help="Target directory path")
args = parser.parse_args()
root_dir = args.dir
if not os.path.isdir(root_dir):
print(f"Error: '{root_dir}' is not a directory.")
return
# Using glob for pattern matching - recursive by default as requested
pattern = os.path.join(root_dir, "**", "*.json")
# glob.glob returns a list of matching file paths
# recursive=True is needed for ** to work properly
files = glob.glob(pattern, recursive=True)
if not files:
print(f"No JSON files found in '{root_dir}' or its subdirectories.")
return
print(f"Found {len(files)} JSON files.")
# Direct deletion as requested for a script tool
deleted_count = 0
for file_path in files:
try:
if os.path.isfile(file_path):
os.remove(file_path)
print(f"Deleted: {file_path}")
deleted_count += 1
except Exception as e:
print(f"Failed to delete {file_path}: {e}")
print(f"Successfully deleted {deleted_count} JSON files.")
if __name__ == "__main__":
main()