如何将两个脚本组合在一起?

时间:2016-03-26 07:02:10

标签: python linux hash scripting hashcode

我写了2个不同的脚本 第一个是从所有.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'

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

如何减少哈希生成中的迭代,将其包装到函数中并在找到新文件时调用它:

import time
import glob
import os
import hashlib

def md5(filename):
    with open(filename, 'rb') as inputfile:
        data = inputfile.read()
        print filename, hashlib.md5(data).hexdigest()

path_to_watch = "."
before = os.listdir(path_to_watch)
while 1:
    time.sleep(3)
    after = 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)
        for filename in added:
            md5(filename)
    if removed: 
        print "Removed: ", ", ".join(removed)
    before = after

还从代码中删除了一些不必要的字典。

我建议你把它作为一个挑战,在保持脚本功能的同时将语句数量和数据转换次数减少到最少。同时,值得一看Python Style Guide;)