-
Notifications
You must be signed in to change notification settings - Fork 131
/
main.py
144 lines (116 loc) · 3.31 KB
/
main.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name: mian
Description :
Author : CoolCat
date: 2019/5/11
-------------------------------------------------
Change Activity:
2019/5/11:
-------------------------------------------------
"""
__author__ = 'CoolCat'
import os
import hashlib
import time
def monitor(dir):
fileDirs = []
for root, dirs, files in os.walk(dir, topdown=False):
for name in files:
# print(os.path.join(root, name))
fileDirs.append(os.path.join(root, name))
return fileDirs
# for name in dirs:
# print(os.path.join(root, name))
def calcMD5(filepath):
try:
with open(filepath, 'rb') as f:
md5obj = hashlib.md5()
md5obj.update(f.read())
hash = md5obj.hexdigest()
# print(hash)
return "修改了" + filepath + " Hash为:" + hash
except:
pass
def help():
print("""
_____________
< FileMonitor >
-------------
/\_)o<
| |
| O . O |
\_____/
By CoolCat
""")
def getDir():
try:
dir = str(raw_input(time.strftime('[%H:%M:%S]:') + "Please enter a directory:"))
except:
dir = str(input(time.strftime('[%H:%M:%S]:') + "Please enter a directory:"))
pass
return dir
if __name__ == '__main__':
help()
try:
dir = str(raw_input(time.strftime('[%H:%M:%S]:') + "Please enter a directory:"))
except:
dir = str(input(time.strftime('[%H:%M:%S]:') + "Please enter a directory:"))
pass
# dir = "../Aliyun/"
print(time.strftime('[%H:%M:%S]:') + "FileMonitor is running...")
# print(dir)
a = monitor(dir)
while True:
#print(len(a))
b = monitor(dir)
#print(len(b))
if len(a) > len(b):
c = list(set(a).difference(set(b)))
try:
# print(len(a))
# print(len(b))
# print(len(c))
print(time.strftime('[%H:%M:%S]:') + "删除了" + str(c[-1]))
# a = b
except:
pass
elif len(b) > len(a):
c = list(set(b).difference(set(a)))
try:
print(time.strftime('[%H:%M:%S]:') + "新建了" + str(c[-1]))
# a = b
except:
pass
elif len(a) == len(b):
# print(len(a))
# print(len(b))
# print(len(c))
aList = []
for pathName in a:
hash = calcMD5(pathName)
aList.append(hash)
#print(aList)
bList = []
for pathName in b:
hash = calcMD5(pathName)
bList.append(hash)
#print(bList)
cList = list(set(aList).difference(set(bList)))
if len(cList) != 0:
try:
# print(len(aList))
# print(len(bList))
# print(len(cList))
print(time.strftime('[%H:%M:%S]:') + cList[-1])
except:
pass
# a = b
else:
pass
a = b
else:
pass
a = b
#break