【问题标题】:To get list of added removed and modified files in directory using python script使用 python 脚本获取目录中添加的删除和修改文件的列表
【发布时间】:2021-08-14 12:02:48
【问题描述】:

我编写了一个 Python 脚本,它将获取目录中所有文件的 md5sum (linux) 现在我想从该目录中获取添加、删除和修改的文件列表

from commands import getoutput
import os
import hashlib
from os import walk
files = []
for dirpath, dirnames, filenames in walk('/home/omkarp/testmd'):
    for file in filenames:
        files.append(os.path.join(dirpath,file))

md5_cmd = "md5sum %s"
md5sum = []
for f in files:
    md5sum.append(getoutput(md5_cmd % (f)))
#print md5sum
dict_md = {}
for d in md5sum:
    var = d.split(" ")
    dict_md[var[2]] = var[0]
print dict_md
fp = open("/home/omkarp/md5sum.txt", 'w')
#for md5 in md5sum:
    #fp.write(md5 + "\n")
fp.write(str(dict_md))
fp.close()

【问题讨论】:

    标签: python linux networking directory md5sum


    【解决方案1】:

    你可以这样做:

    import ast
    
    fp = open("md5sum.txt", 'r')
    contents = fp.read()
    
    dictionary = ast.literal_eval(contents) # evaluates file content as a dictionnary
    
    for key, value in dict_md.items():
        try:
            sum = dictionary[key]
        except KeyError: # If the key is not in the dictionnary
            print "New file"
        if sum!=value: # If the md5sum is different
            print "File modified"
    

    【讨论】:

      猜你喜欢
      • 2016-03-30
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多