On Wednesday, 12 July 2017 02:32:29 UTC+10, Ganesh Pal wrote: > Dear Python friends > > I am trying to open a file and check if there is a pattern has changed > after the task got completed? > > file data: > ........................................................ > > #tail -f /file.txt > .......................................... > Note: CRC:algo = 2, split_crc = 1, unused = 0, initiator_crc = b6b20a65, > journal_crc = d2097b00 > Note: Task completed successfully. > Note: CRC:algo = 2, split_crc = 1, unused = 0, initiator_crc = d976d35e, > journal_crc = a176af10 > > > I have the below piece of code but would like to make this better more > pythonic , I found regex pattern and exception handling poor here , any > quick suggestion in your spare time is welcome. > > > #open the existing file if the flag is set and check if there is a match > > log_file='/file.txt' > flag_is_on=1 > > data = None > with open(log_file, 'r') as f: > data = f.readlines() > > > if flag_is_on: > logdata = '\n'.join(data) > reg = "initiator_crc =(?P<ini_crc>[\s\S]*?), journal_crc" > crc = re.findall(re.compile(reg), logdata) > if not crc: > raise Exception("Pattern not found in logfile") > > checksumbefore = crc[0].strip() > checksumafter = crc[1].strip() > logging.info("checksumbefore :%s and checksumafter:%s" > % (checksumbefore, checksumafter)) > > if checksumbefore == checksumafter: > raise Exception("checksum not macthing") > > I am on Linux and Python 2.7 > > Regards, > Ganesh
There's not much need to compile regexes unless you've got *a lot* of them in your code. The first ones are automatically compiled and cached: https://stackoverflow.com/questions/452104/is-it-worth-using-pythons-re-compile Cheers, Nick -- https://mail.python.org/mailman/listinfo/python-list