вторник, 21 июня 2011 г.

python script which finds hex values in a file

Recently I needed to parse a few log files and then to compare them in WinMerge utility. Files had different structure, but they both contained HEX values which had to be verified for compliance.

Thus, I created a simple python script ( thanx Google :) )  which looks for hex values in a file using regexp . Actually, this script walks through a specified folder where files are and then creates parsed output files containing HEX values with an additional '_hex.txt' extension in their names

Here is a script:

import os
import re
def look_for_hex_in_files(filepath,writefilepath):
    f = open(filepath, 'r')
    fwr = open(writefilepath, 'w')
    for line in f:
        b = line
        for b1 in re.finditer('0x0*[0-9a-fA-F][0-9a-fA-F]*', b):
            a = (b1.group(0))
            print(a)
            fwr.write(a+'\n')
    f.close()
    fwr.close()

#this is a path to folder containing input log files to be parsed
dirname = "C:\ProgramData\log1"

for filename in os.listdir(dirname):
    b1 = (os.path.join(dirname,filename))
    if os.path.isfile(b1):
        print(b1)
        filepath = b1;
        writefilepath = (b1+"_hex.txt")
        look_for_hex_in_files(filepath,writefilepath)