File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ import datetime
2
+ import hashlib
3
+ import os
4
+
5
+
6
+ def get_file_hash (src_file ):
7
+ with open (src_file , 'rb' ) as f :
8
+ line = f .readline ()
9
+ md5_hash = hashlib .md5 ()
10
+ while line :
11
+ md5_hash .update (line )
12
+ line = f .readline ()
13
+ return md5_hash .hexdigest ()
14
+
15
+
16
+ def get_bytes_hash (src_file , Bytes = 1024 ):
17
+ md5_hash = hashlib .md5 () # 创建一个md5算法对象
18
+ with open (src_file , 'rb' ) as f : # 打开一个文件,必须是'rb'模式打开
19
+ while 1 :
20
+ data = f .read (Bytes ) # 由于是一个文件,每次只读取固定字节
21
+ if data : # 当读取内容不为空时对读取内容进行update
22
+ md5_hash .update (data )
23
+ else : # 当整个文件读完之后停止update
24
+ break
25
+ ret = md5_hash .hexdigest () # 获取这个文件的MD5值
26
+ return ret
27
+
28
+
29
+ def traverse_file (src_dir ):
30
+ start = datetime .datetime .now ()
31
+ file_num = 0
32
+ if os .path .exists (src_dir ):
33
+ for root , dirs , files in os .walk (src_dir ):
34
+ for file in files :
35
+ file_num = file_num + 1
36
+ src_file = os .path .join (root , file )
37
+ print (
38
+ file + "-->file_hash-->" + get_file_hash (src_file ) + "-->bytes_hash-->" + get_bytes_hash (src_file ))
39
+
40
+ end = datetime .datetime .now ()
41
+ print ('耗时: %s Seconds' % (end - start ))
42
+
43
+
44
+ if __name__ == "__main__" :
45
+ srcDir = os .path .abspath (r"/Users/xhzh/yxFiles/_pic/video" )
46
+ traverse_file (srcDir )
You can’t perform that action at this time.
0 commit comments