-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSorter.py
More file actions
42 lines (26 loc) · 1.34 KB
/
FileSorter.py
File metadata and controls
42 lines (26 loc) · 1.34 KB
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
import os, shutil
path = r"/Users/calle/Desktop/Pythonproject" #Defining path using r to interpret the string as literal to avoid newline for example.
files = os.listdir(path) #list directories in path
print(files)
folder_names = ["csv files","image files", "text files"] #creating names for folders
for loop in range(len(folder_names)): #loop which checks if folders exists if not it will create it via folder_names list
full_path = os.path.join(path, folder_names[loop])
if not os.path.exists(full_path):
os.makedirs(full_path)
print("Folder created", full_path)
for file in files:
if ".csv" in file:
source = os.path.join(path, file)
destination_csv = os.path.join(path, "csv files")
if not os.path.exists(path + "csv files/" + file):
shutil.move(source, destination_csv)
elif ".webp" in file or ".jpg" in file:
source = os.path.join(path, file)
destination_image = os.path.join(path, "image files")
if not os.path.exists(path + "image files/" + file):
shutil.move(source,destination_image)
elif ".docx" in file or ".pdf" in file:
source = os.path.join(path, file)
destination_text = os.path.join(path, "text files")
if not os.path.exists(path + "text files/" + file):
shutil.move(source, destination_text)