I’ve written this small piece of code:
import csv
import re
import os
fileobj = csv.reader(open('c:\\paths1.csv', 'rb'), delimiter=' ', quotechar='|')
for row in fileobj:
for x in row:
with open(x) as f:
for line in f:
if re.match('(.*)4.30.1(.*)', line):
print 'The version match: '+ line
print 'incorrect version'
filesize= os.path.getsize(x)
print 'The file size is :'+ str(filesize) +' bytes';
What I would like to make it do is:
Add exception handling, as far as I know if the method match() doesn’t
match anything in the file returns the value None, however I didn’t quite understand
how to read that value to make a comparison and let the script print (the version does not match)…
Anyone has any suggestion? It wouldn’t be bad also to have some link to some web documentation.
Thank you in advance!
You’re on the right way. As boolean value of
Noneis False, all you have to do is use anelsebranch in your code:now i’m pretty sure you either want to match the first (the one that contains version number) line of the file or the whole file, so just in case: