我写了两个不同的剧本 第一个是从所有.exe文件中获取一个md5哈希 另一个脚本是某个代理,如果他们的文件是新的,则每隔3秒检查一次 在目录中。 现在我需要让代理检查文件并打印每个md5
这是我的剧本:
import os, time
path_to_watch = "/root/PycharmProjects/untitled1"
before = dict ([(f, None) for f in os.listdir (path_to_watch)])
while 1:
time.sleep (3)
after = dict ([(f, None) for f in os.listdir (path_to_watch)])
added = [f for f in after if not f in before]
removed = [f for f in before if not f in after]
if added: print "Added: ", ", ".join (added)
if removed: print "Removed: ", ", ".join (removed)
before = after
第二个负责检查md5
import glob
import os
import hashlib
work_path = '/root/PycharmProjects/untitled1/'
filenames = glob.glob("/root/PycharmProjects/untitled1/*.exe" )
if len(os.listdir(work_path)) > 0:
for filename in filenames:
with open(filename, 'rb') as inputfile:
data = inputfile.read()
print hashlib.md5(data).hexdigest()
else:
print '0'
谢谢你的帮助!你知道吗
如何减少哈希生成的迭代,将其包装到函数中,并在找到新文件时调用它:
还从代码中删除了一些不必要的dict内容。你知道吗
我建议您将其视为一个挑战,在保持脚本功能的同时,将语句的数量和数据转换的数量减少到最小。同时也值得一看Python Style Guide;)
相关问题 更多 >
编程相关推荐