-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfolder_organiser.py
170 lines (140 loc) · 5.02 KB
/
folder_organiser.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
158
159
160
161
162
163
164
165
166
167
168
169
170
"""
python-folder-organiser
This script automatically organise all the files of a folder named 'Misc' by moving them in various folders depending
on their extensions.
Author: Ama
"""
import os
import sys
import time
#python dictionary containing all folder names and relative file extensions
FOLDER_TYPES = {
"Music": [
'.mp3', '.m4a', '.flac', '.wav', '.wma', '.aac'
],
"Videos": [
'.avi', '.mpg', '.mp2', '.mp4', '.mpeg', '.mpe', '.mpv', '.mkv', '.m4p', '.m4v', '.wmv', '.mov', '.qt', '.swf', '.flv'
],
"Documents": [
'.doc', '.docx', '.xls', '.xlsx', '.txt', '.odt', '.ods', '.ppt', '.pptx', '.html', '.htm'
],
"PDFs": [
'.pdf'
],
"Photos": [
'.tif', '.tiff', '.bmp', '.jpg', '.jpeg', '.gif', '.png', '.eps', '.raw', '.cr2', '.nef', '.orf', '.sr2'
]
}
def logo_print():
print("\n\n")
print("=" * 40)
print(" **** * * ***** * * *** * *")
time.sleep(0.2)
print(" * * * * * * * * * ** *")
time.sleep(0.2)
print(" **** * * **** * * * * *")
time.sleep(0.2)
print(" * * * * * * * * **")
time.sleep(0.2)
print(" * * * * * *** * *")
time.sleep(0.2)
print(" " + "-" * 38)
time.sleep(0.3)
print(" F O L D E R O R G A N I S E R")
print("=" * 40)
print("\n\n")
time.sleep(0.3)
def show_instructions():
print("INSTRUCTIONS")
print("1. Create a folder named 'Misc' in the same directory as the script \n" \
"(or just run the program the first time and the folder will be created for you)")
print("2. Put all the files you want to organise inside the 'Misc' folder")
print("3. Execute the script")
print("4. Enjoy the tidy result\n")
def get_file_names():
"""
This function returns a list containing full pathname of each file present in the 'Misc' folder
"""
current_dir = os.getcwd() #current directory
misc_dir = os.path.join(os.getcwd(), 'Misc')
#Check if the folder 'Misc' already exists and if not, it proceeds to create it
if not os.path.isdir(misc_dir):
os.mkdir(misc_dir)
#List of all files in the 'Misc' folder
#Each element in the list is a set containing full file path and filename
file_list = [(os.path.join(misc_dir,file), file) for file in os.listdir(misc_dir) if os.path.isfile(os.path.join(misc_dir, file))]
return file_list
def organise_file(filepath, filename):
"""
Given a file path and name, this function checks the file extension and moves the file to the corresponding folder
"""
file, extension = os.path.splitext(filename)
destination_folder = ""
for fn in FOLDER_TYPES:
if extension.lower() in FOLDER_TYPES[fn]:
destination_folder = os.path.join(os.getcwd(), fn)
break
#If extension is not in my extensions list, just return an empty string and leave the file in the 'Misc' directory
if destination_folder == "":
return "passed"
#Creates the folder if doesn't exist
if not os.path.isdir(destination_folder):
os.mkdir(destination_folder)
#Move the file from the original folder to the destination folder mantaining the same file name
#checks if file already exists to avoid unwanted overwriting
new_filepath = os.path.join(os.getcwd(), destination_folder, filename)
if not os.path.isfile(new_filepath):
os.rename(filepath, new_filepath)
return destination_folder
if __name__ == "__main__":
"""
Main execution
"""
#Logo print
logo_print()
#Asking user input
print("Choose an option")
while True:
print("\n")
print("i - See Instructions")
print("o - Organise files in folder 'Misc'")
print("q - Quit program")
usr_option = input("option: ").lower()
if usr_option == "q":
sys.exit()
elif usr_option == "i":
show_instructions()
time.sleep(0.3)
print("Choose an option")
elif usr_option == "o":
break
else:
print("Wrong choice...please select one of the following options:")
print("Perusing your files...")
file_list = get_file_names()
file_tot = len(file_list) #Total number of files in 'Misc'
print("Moving files around...")
folders = {}
#for loop to move each file and save the counter for final stats
for f in file_list:
folder = organise_file(f[0], f[1])
if folder in folders.keys():
folders[folder] += 1
else:
folders[folder] = 1
print("Finished!")
#Final Stats
print("=" * 11)
print(" S T A T S")
print("=" * 11)
time.sleep(0.2)
print(f"Processed a total of {file_tot} files")
for f in folders:
if f != "passed":
time.sleep(0.2)
print(f" # {folders[f]} file(s) moved in folder {f}")
if "passed" in folders.keys():
time.sleep(0.2)
print(f"WARNING! # {folders['passed']} file(s) left unmoved...")
time.sleep(0.2)
print("\nBye! :)\n")