nassio
nassio

Reputation: 645

I would like to handle an exception in case of re.match returns null

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!

Upvotes: 0

Views: 73

Answers (3)

soulcheck
soulcheck

Reputation: 36777

You're on the right way. As boolean value of None is False, all you have to do is use an else branch in your code:

 if re.match('(.*)4.30.1(.*)', line):
            print 'The version match: '+ line
 else:
            print 'incorrect version'

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:

      #first line
      with open(x) as f:
           try:
               #next(f) returns the first line of f, you have to handle the exception in case of empty file
               if re.match('(.*)4.30.1(.*)', next(f))):
                    print 'The version match: '+ line
               else:
                    print 'incorrect version'
           except StopIteration:
               print 'File %s is empty' % s


      #anything
      with open(x) as f:
           if re.match('(.*)4.30.1(.*)', f.read())):
                print 'The version match: '+ line
           else:
                print 'incorrect version'

Upvotes: 2

John Machin
John Machin

Reputation: 82992

import csv
import re #### don't need it
import os #### don't need it
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 '4.30.1' in line: #### much simpler than regex
                    print 'The version match: '+ line
                    break
            else: # Yes, a `for` statement can have an `else:` 
                # end of file, "break" doesn't arrive here
                print 'incorrect version' # done ONCE at end of file

Upvotes: 1

RanRag
RanRag

Reputation: 49577

>>> st = "hello stackers"
>>> pattern = "users"
>>> if re.match(pattern,st):
...      print "match found"
... else:
...     print "no match found"
...
no match found
>>>

Because re.match() returns true if match found.So,just use an else statement if no match is found.

Upvotes: 0

Related Questions