-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_files.py
70 lines (52 loc) · 2 KB
/
sort_files.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
# For sorting and arranging files into subfolders based on file type #
# Author: Pratiksha Jain
# Created On: 13.06.21
#-------------------------------#
# Importing required libraries
import os
import glob
import shutil
#-------------------------------#
# Edit the exact folder name here, with path
folder = "/home/folder/name"
#-------------------------------#
# Dictionary of types of folders and the document extensions - if needed, edit according to your needs
filename = glob.glob(folder+"*")
doctype_dict = {
'pdfs':['.pdf', '.PDF'],
'docs':['.docx','.doc','.txt', '.ppt', '.pptx', '.md'],
'images':['.jpeg','.jpg','.JPG','.svg','.png','.PNG', '.gif', '.psd'],
'videos' : ['.mp4','.mp3', '.flb'],
'setupFiles':['.exe','.msi', '.apk', '.EXE'],
'compressedFiles':['.zip', '.rar'],
'python codes': ['.py'],
'setupFiles_Debian':['.deb'],
'spreadsheets':[ '.xls', '.xlsx', '.csv', '.ods']
}
#-------------------------------#
# The actual code
for file in filename:
# getting extension of file
file_base = file.replace(folder,'')
ext = os.path.splitext(file_base)[1]
# Matching extension with dict values
for key, value in doctype_dict.items():
if ext in value:
# Defining location of subfolder
loc = folder+key
# in case subfolder does not exist already
if not (os.path.exists(loc)):
os.mkdir(loc)
# moving file into subfolder
try:
shutil.move(file,loc)
except shutil.Error as e:
print(e.args[0])
# Error: File with same name already exists in that folder
if 'already exists' in e.args[0]:
file_1 = file.replace(ext, '_1'+ext)
os.rename(file, file_1)
shutil.move(file_1,loc)
print("File can be found as %s_1"%file_base.replace(ext,''))
break
#-------------------------------#