-
Notifications
You must be signed in to change notification settings - Fork 1
/
JRS_RemoveOphans.py
64 lines (39 loc) · 1.7 KB
/
JRS_RemoveOphans.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'''
Removes jpg files that do not have a matching txt file
Removes txt files that do not have a matching jpg file.
A dialog box will prompt for the source folder which contains the files to be checked for orphan files.
'''
import os
import sys
from tkinter import filedialog
from tkinter import *
from absl import app, flags, logging
from absl.flags import FLAGS
flags.DEFINE_boolean('delete_mode', False, '\nBy default the program only reports which orphan files should be deleted.\nIf delete_mode flag is set then program will actually delete orphans files.')
def main(_argv):
root = Tk()
root.withdraw()
PATH = filedialog.askdirectory(title = "Select Target Directory")
try:
filenames = os.listdir(PATH)
except:
print("No target directory was selected.")
sys.exit()
for i in range(0, len(filenames)):
# if it ends in .txt
if filenames[i].endswith(".txt"):
if filenames.count(filenames[i].replace(".txt", ".jpg")) != 1:
print(filenames[i])
if FLAGS.delete_mode == True:
os.remove(PATH + "/" + filenames[i])
# if it ends in .txt
if filenames[i].endswith(".jpg"):
if filenames.count(filenames[i].replace(".jpg", ".txt")) != 1:
print(filenames[i])
if FLAGS.delete_mode == True:
os.remove(PATH + "/" + filenames[i])
if __name__ == '__main__':
try:
app.run(main)
except SystemExit:
pass