0

次のコードを書きましたが、動作させることができず、その理由もわかりません。コード:

  1. 対象ファイルのリストを読み込みます
  2. ディレクトリをループします
  3. ファイルで MD5 ハッシュを実行します
  4. 上記のファイルの以前の md5 ハッシュについてアクティブファイルをチェックします
  5. ファイルが新しい場合は、新しいものとしてログに記録されます
  6. ログが存在するが変更された場合、変更が書き込まれ、変更がログに記録されます
  7. 新しくなく変更がない場合は何もしない

コードは次のとおりです。

import hashlib
import logging as log
import optparse
import os
import re
import sys
import glob
import shutil

def md5(fileName):
    """Compute md5 hash of the specified file"""
    try:
        fileHandle = open(fileName, "rb")
    except IOError:
        return
    m5Hash = hashlib.md5()
    while True:
        data = fileHandle.read(8192)
        if not data:
            break
        m5Hash.update(data)
    fileHandle.close()
    return m5Hash.hexdigest()

req = open("requested.txt")
for reqline in req:
    reqName = reqline[reqline.rfind('/') + 1:len(reqline) - 1]
    reqDir = reqline[0:reqline.rfind('/') + 1] 
    tempFile = open("activetemp.txt", 'w') 
    for name in glob.glob(reqDir + reqName):    
        fileHash = md5(name) 
        actInt = 0
        if fileHash != None:

            actFile = open("activefile.txt")

            for actLine in actFile:
                actNameDir = actLine[0:actLine.rfind(' : ')]
                actHash = actLine[actLine.rfind(' : ') + 3:len(actLine) -1]
                if actNameDir == name and actHash == fileHash:
                    tempFile.write(name + " : " + fileHash + "\n")
                    actInt = 1 
                    print fileHash
                    print actHash
                    print name
                    print actNameDir
                if actNameDir == name and actHash != fileHash:
                    fimlog = open("fimlog.txt", 'a')
                    tempFile.write(name + " : " + actHash + "\n")         
                    actInt = 1
                    fimlog.write("FIM Log: The file " + name +  " was modified: " + actHash + "\n") 
            if actInt == 0: 
                fimlog = open("fimlog.txt", 'a')
                fimlog.write("FIM Log: The file " + name +  " was created: " + fileHash + "\n")
                tempFile.write(name + " : " + fileHash + "\n")                       

shutil.copyfile("activetemp.txt", "activefile.txt")
4

1 に答える 1

1

問題を実際に説明したことはありませんが、考えられる原因の 1 つは、tempFile (またはその他のファイル) を決して閉じないため、最後のファイル コピーが失敗する可能性があることです。

于 2013-02-18T01:53:44.903 に答える