This repository was archived by the owner on Nov 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathautomate_file_classification.py
72 lines (51 loc) · 1.8 KB
/
automate_file_classification.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
71
72
#the rules.txt file contains the necessary info and rules for moving the files
import os
import shutil
#the following lines indicate the path of the rules.txt file
file_path = os.getcwd()
file_name = os.path.join(file_path, "rules.txt")
#get the path and the mode from rules.txt
file_t = open(file_name,'r')
path = file_t.readline()
mode = file_t.readline()
mode = mode.strip("\n")
path1 = path.strip("\n")
#the following function returns a dictionary that contains the rules. The dictionary's keys behave as extensions of the files and values act as moved paths
dict1 = {}
def rules():
dict1 = {}
for each in file_t:
each = each.strip("\n")
if each.split(":",1)[0]:
file_ext,dest_path = each.split(":",1)
file_ext = file_ext.strip()
dest_path = dest_path.strip()
dict1[file_ext] = dest_path
return dict1
#the following function takes a list of files and moves the files to their resp files
def file_move(files_list):
for file in files_list:
if "." in file:
ext = file.rsplit(".",1)[1]
ext = ext.strip()
if ext in dict1:
dst = dict1[ext]
try:
print (file)
shutil.move(file,dst)
except Exception as e:
print (e)
#the following function is used when a simple mode is selected
def single_dir(path1):
os.chdir(path1)
files = os.listdir(".")
file_move(files)
#the following function is used when recursive mode is selected
def rec_dirs(path1):
for root,dirs,files in os.walk(path1,topdown=True,onerror=None,followlinks=False):
#print files
os.chdir(root)
file_move(files)
print("files are moved")
dict1 = rules()
rec_dirs(path1)