-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
84 lines (69 loc) · 2.69 KB
/
Copy pathmain.py
File metadata and controls
84 lines (69 loc) · 2.69 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
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
import os #shows files
import shutil #actions
import sys #CLI
import json
from utilities import unknown_file
from utilities import already_existing_file
from utilities import undo_function
#functions of os i know listdir==to get list of files
#os.path.isfile==it checks the selection is file or not
#os.path.join==it helps to define correct path
#os.path.exists==y
#folders_path=sys.argv[1] #path defined
#
#if not folders_path: Not working trying another
# print("Enter folder Path!")
#
try:
folder_path = sys.argv[1]
except IndexError:
print("Folder path not provided")
sys.exit()
if not os.path.exists(folder_path):
sys.exit("Folder Path Not Found")
class FileOrganizer:
def __init__(self,folder_path):
self.folder_path=folder_path
def organize_files(self):
files=os.listdir(self.folder_path) #getting list of files
with open('log.txt', 'a') as log:
with open('data.json', 'r+') as Ex_file:
ext_map = json.load(Ex_file)
log.write("--BREAK--\n")
for file in files:
source_path = os.path.join(self.folder_path, file)
if not os.path.isfile(source_path):
continue
ext=os.path.splitext(file)[1].lower()
folder_name=ext_map.get(ext)
if folder_name is None:
new_exti=unknown_file(ext)
for k,v in new_exti.items():
ext_map[k]=v
Ex_file.seek(0)
json.dump(ext_map, Ex_file)
Ex_file.truncate()
#log.write(f'unknown file extintion:{file} added to dict\n ')
folder_name=v
destination_path = os.path.join(self.folder_path, folder_name)
if not os.path.exists(destination_path):
os.makedirs(destination_path)
final_destination_path = os.path.join(destination_path, file)
if os.path.exists(final_destination_path):
new_path=already_existing_file(file,destination_path)
try:
os.rename(source_path,new_path)
except OSError:
shutil.move(source_path,new_path)
log.write(f'{source_path}->{new_path}\n')
else:
try:
os.rename(source_path,final_destination_path)
except OSError:
shutil.move(source_path, final_destination_path)
log.write(f"{source_path}->{final_destination_path}\n")
if len(sys.argv)>2 and sys.argv[2]=="--undo":
undo_function()
else:
organize=FileOrganizer(folder_path)
organize.organize_files()