-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsortImages.py
28 lines (22 loc) · 878 Bytes
/
sortImages.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
import os
import shutil
mainFolder = "main/folder/toSort"
# image name example : MRV01-003-000-002.jpg
def main():
# sort files by last code name in file name
for filenames in os.listdir(mainFolder):
if filenames.endswith(".jpg"):
# file name without extension
fileName = filenames.split(".")[0]
# slice file name by - to get last code name
codeName = int(fileName.split("-")[-1])
print(codeName)
# create new folder with codename
newFolder = mainFolder + "/" + str(codeName)
# create new folder if it doesn't exist
if not os.path.exists(newFolder):
os.makedirs(newFolder)
# move file to new folder
shutil.move(mainFolder + "/" + filenames, newFolder + "/" + filenames)
if __name__ == '__main__':
main()