-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrop-extraneous.py
executable file
·43 lines (36 loc) · 1.18 KB
/
drop-extraneous.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
#!/usr/bin/env python3
# -*- coding: utf8 -*-
import argparse
import os
import os.path
import sys
def main():
ap = argparse.ArgumentParser(
description="delete extraneous files with given suffix",
)
ap.add_argument("dir", help="directory to process")
ap.add_argument(
"-e",
"--extension",
required=True,
help="file extension of extraneous files to be deleted")
ap.add_argument(
"-d",
"--dry-run",
action="store_true",
help="dry run, only shows which files would be deleted")
args = ap.parse_args()
for root, dirs, files in os.walk(args.dir):
file_dict = {}
for filename in files:
fn_root, fn_ext = os.path.splitext(filename)
file_dict.setdefault(fn_root, []).append(fn_ext)
for fn_root, fn_ext_list in file_dict.items():
if len(fn_ext_list) > 1 and args.extension in fn_ext_list:
full_path = os.path.join(root, fn_root + args.extension)
if args.dry_run:
print(full_path)
else:
os.remove(full_path)
if __name__ == '__main__':
sys.exit(main())