简体   繁体   English

使用 python 脚本获取目录中添加的删除和修改文件的列表

[英]To get list of added removed and modified files in directory using python script

I wrote a Python script that will get the md5sum of all files in a directory(linux) now i want to get list of added and removed and modified files from that directory我写了一个 Python 脚本,它将获取目录(linux)中所有文件的 md5sum 现在我想从该目录中获取添加、删除和修改文件的列表

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()

You could do something like:您可以执行以下操作:

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM