Python - Attribute error, 'NoneType' object has no attribute
<p>So I have a program that reason a lines file and prints out any errors to stderr. </p>

<pre><code>for line in lines_file:
    #get offset up to start of coordinates
    start = re.compile('\s*line\s*')
    m = start.match(line)
    offset = m.end()

    try:
        for i in range(4):
            xy = re.compile('\s*([-]?[0-9]{1,3})\s*')

            if xy.match(line,offset):
                m = xy.match(line,offset)
            else:
                raise Exception

            coordinate = m.group(1)

            if int(coordinate) &gt; 250 or int(coordinate) &lt; -250:
                raise Exception

            offset = m.end()

        end = re.compile('\s*$')
        if not end.match(line,offset):
            raise Exception

    except Exception as e:
        print &gt;&gt; sys.stderr, 'Error in line ' + str(line_number) + ":"
        print &gt;&gt; sys.stderr, " " * 4 + line,
        print &gt;&gt; sys.stderr, " " * (offset + 4) + "^"
        line_number = line_number + 1
        continue 
</code></pre>

<p>And if I put in invalid input lines expecting the invalid lines to be printed to stderr, the output I get is:</p>

<pre><code>Traceback (most recent call last):
  File "lines_to_svg.py", line 37, in &lt;module&gt;
    offset = m.end()
AttributeError: 'NoneType' object has no attribute 'end'
</code></pre>

<p>Because this is a fragment of my code, line 37 is the one that says <code>offset = m.end()</code>. So why do I keep getting an attribute error? Here's the code that comes before the for loop above just in case this is causing the error:</p>

<pre><code>import sys
import re

# SVG header with placeholders for canvas width and height
SVG_HEADER = "&lt;svg xmlns=\"http:#www.w3.org/2000/svg\" version=\"1.1\""" width=\"%d\" height=\"%d\"&gt;\n"

# SVG bounding box with placeholders for width and height
SVG_BOUNDING_BOX = "&lt;rect x=\"0\" y=\"0\" width=\"%d\" height=\"%d\""" style=\"stroke:#000;fill:none\" /&gt;\n"

# SVG line with placeholders for x0, y0, x1, y1
SVG_LINE = "&lt;line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\""" style=\"stroke:#000\" /&gt;\n"

# SVG footer
SVG_FOOTER = "&lt;/svg&gt;"

CANVAS_HEIGHT = 500
CANVAS_WIDTH = 500

# process command line arguments
if len(sys.argv) != 2:
    print &gt;&gt; sys.stderr, "Usage:", str(sys.argv[0]), "lines_file"
    exit(1)

#open file for reading
try:
    lines_file = open(sys.argv[1], 'r')
except IOError:
    print &gt;&gt; sys.stderr, "Cannot open:", str(sys.argv[1])
    exit(2)

offset = 0
line_number = 1
</code></pre>

<p>The problem is with <code>offset = m.end()</code> but I can't seem to figure out why that's causing the error. </p>