-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_manager.py
65 lines (46 loc) · 2 KB
/
file_manager.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
from os.path import dirname, abspath
import os
import random
import shutil
class File_Manager:
dir_to_watch = dirname(dirname(abspath(__file__)))
def file_manage(self):
obj = os.scandir(self.dir_to_watch)
for entry in obj:
if entry.is_file():
print("file created")
self.file_types(entry)
def file_types(self, file):
file_name, file_extension = os.path.splitext(file.name)
if file_extension == '.txt':
self.tranfer_file(file, 'text');
return None;
if file_extension == '.doc' or file_extension == '.docx' or file_extension == '.odt' or file_extension == '.pdf':
self.tranfer_file(file, 'documnetations')
return None;
if file_extension == '.img' or file_extension == '.png' or file_extension == '.JPEG' or file_extension == '.svg' or file_extension == '.jpg':
self.tranfer_file(file, 'images')
return None;
if file_extension == '.csv':
self.tranfer_file(file, 'dataSets')
return None;
self.tranfer_file(file, 'others');
def tranfer_file(self, file, dir_name):
dir_path = os.path.join(self.dir_to_watch, dir_name)
if not os.path.exists(dir_path):
os.mkdir(dir_path)
reallocated_file_path = os.path.join(dir_path, file.name)
if os.path.exists(reallocated_file_path):
random_num =random.random();
random_str = str(random_num)
file_name, file_extension = os.path.splitext(file.name)
change_name = file_name+random_str+file_extension;
reallocated_file_path = os.path.join(dir_path,change_name)
try:
shutil.copyfile(file.path, reallocated_file_path)
except:
print("error occur when copying file");
try:
os.remove(file.path)
except:
print("error occur when deleting file");