import getopt
import hashlib
import sys
import os
_FILE_SLIM = 100 * 1024 * 1024
filePaths = ['/webapp01/www/ydb3', '/webapp02/qhwebapps/qhmcc_client9681', '/webapp02/qhwebapps/qhmcc_client9682',
'/webapp02/qhwebapps/qhmcc_manager', '/webapp02/qhwebapps/qhmcc_manager9882',
'/webapp02/qhwebapps/qhmcc_wap9781', '/webapp02/qhwebapps/qhmcc_wap9782', '/webapp02/webapps']
fileOutPath = "/webapp07/nfs/sec_lqlog/69_file_hash_.log"
def File_md5(filename):
calltimes = 0
hmd5 = hashlib.md5()
fp = open(filename, "rb")
f_size = os.stat(filename).st_size
result = ""
if f_size > _FILE_SLIM:
while (f_size > _FILE_SLIM):
hmd5.update(fp.read(_FILE_SLIM))
f_size /= _FILE_SLIM
calltimes += 1
if (f_size > 0) and (f_size <= _FILE_SLIM):
hmd5.update(fp.read())
else:
hmd5.update(fp.read())
return str(hmd5.hexdigest())
def wirter(file_and_hash):
with open(fileOutPath, 'a') as f:
f.write(file_and_hash + '\n')
for dir in filePaths:
print "开始计算=========>" + dir
for fpathe, dirs, fs in os.walk(dir):
for f in fs:
try:
file_hash = File_md5(os.path.join(fpathe, f))
file_is_hash = os.path.join(fpathe, f) + "|" + file_hash
print file_is_hash
wirter(file_is_hash)
except Exception as e:
pass
print "69_SUCCESS"