-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_organizer.py
157 lines (131 loc) · 6.12 KB
/
file_organizer.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import os
import sys
import shutil
#Put the files in there
def file_sort(path, filenames, series, exclusions):
count = 0
for file in filenames:
if os.path.isfile(os.path.join(path, file)):
skip = False
for unwanted in exclusions:
if unwanted.upper() in file.upper():
skip = True
break
if skip == False:
if series.upper() in file.upper():
sourcePath = path+"/"+file
destinationPath = path+"/"+series+"/"+file
shutil.move(sourcePath, destinationPath)
count += 1
print("All done! "+ str(count) +" file(s) moved.")
def create_new_dir(path, series="", directories=None):
if directories == None:
directories = []
while series == "":
series = input("Enter the keyword of the files you would like to make a directory for.\n"
"(e.g. if you have pictures of dogs and each file has the word \"Dog\" in it, you would enter Dog): ")
for root, dirs, files in os.walk(path):
directories = dirs
break # Break after the first iteration to get only the top-level directories
for directory in directories:
if series.upper() in directory.upper():
if input("Directory " + directory + " already exists. Do you want to add to a previously existing directory (Y/N)? ").upper() == "Y":
sort_old_dir(path, series, directories)
return
series = ""
break
os.mkdir(path+"/"+series)
exclude = input("Very good. If you would like to exclude any keywords, enter them seperated with /'s\n"
"(e.g. if you want to make an album of images from the year, but exclude pics from the months of June and May\n"
", you would enter: \"June/May\"), otherwise enter \"N\": ")
print("Working...")
exclusions = []
if not exclude.upper() == "N":
exclusions = exclude.strip().split('/')
filenames = os.listdir(path)
file_sort(path, filenames, series, exclusions)
repeat = input("Would you like to keep working in this directory (Y/N/Quit)? ").upper()
if(repeat == "Y"):
while True:
which_sort = input("Would you like to:\n"
"1. Make a new folder.\n"
"2. Add new files into a preexisting directory.\n"
"Select an option (Enter 1 or 2): ")
if which_sort == '1':
create_new_dir(path)
break # exit the loop after create_new_dir
elif which_sort == '2':
sort_old_dir(path)
break # exit the loop after sort_old_dir
else:
print("Invalid response. Enter 1 or 2.")
elif(repeat == "N"):
main()
def sort_old_dir(path, series="", directories=None):
if directories == None:
directories = []
while series == "":
series = input("Enter the keyword of the files you would like to put in an existing directory\n"
"(e.g. if you have pictures of dogs and each file has the word \"Dog\" in it, you would enter Dog): ")
for root, dirs, files in os.walk(path):
directories = dirs
break # Break after the first iteration to get only the top-level directories
found = False
for directory in directories:
if series.upper() in directory.upper():
found = True
break
if found == False:
if input("Directory " + series + " does not exist. Do you want to make a brand new directory (Y/N)?").upper() == "Y":
create_new_dir(path, series, directories)
return
series = ""
exclude = input("Very good. If you would like to exclude any keywords, enter them seperated with /'s\n"
"(e.g. if you want to make an album of images from the year, but exclude pics from the months of June and May\n"
", you would enter: \"June/May\"), otherwise enter \"N\": ")
print("Working...")
exclusions = []
if not exclude.upper() == "N":
exclusions = exclude.strip().split('/')
filenames = os.listdir(path)
file_sort(path, filenames, series, exclusions)
repeat = input("Would you like to keep working in this directory (Y/N/Quit)? ").upper()
if(repeat == "Y"):
while True:
which_sort = input("Would you like to:\n"
"1. Make a new folder.\n"
"2. Add new files into a preexisting directory.\n"
"Select an option (Enter 1 or 2): ")
if which_sort == '1':
create_new_dir(path)
break # exit the loop after create_new_dir
elif which_sort == '2':
sort_old_dir(path)
break # exit the loop after sort_old_dir
else:
print("Invalid response. Enter 1 or 2.")
elif(repeat == "N"):
main()
def main():
path = input("Enter the directory you would like to work in: ")
if os.path.exists(path):
print("Path is valid.")
if not os.path.isdir(path):
print("However, path is not a directory.")
sys.exit()
which_sort = 0
while not (which_sort == '1' or which_sort == '2'):
which_sort = input("Would you like to:\n"
"1. Make a new folder.\n"
"2. Add new files into a prexisting directory.\n"
"Select an option (Enter 1 or 2): ")
if not (which_sort == '1' or which_sort == '2'):
print("Invalid response. Enter 1 or 2.")
if which_sort == '1':
create_new_dir(path)
if which_sort == '2':
sort_old_dir(path)
if __name__ == "__main__":
main()
print("Goodbye!")
sys.exit