impfile = filename[:4]
with open(filename + '_earmark.dat', 'w') as media:
    with open(impfile, 'r') as f:
        InvNo = URL = None
for line in f:
    if line.startswith('InvNo: '):
        InvNo = line[line.find('InvNo: ') + 7:len(line)]
if line.startswith('Media: '):
    mediaref = line[7:len(line) - 1]
URL = getURL(mediaref)
if line.startswith('~EOR~'):
    if InvNo and URL:
        recordbuf = 'IN {}\nUR {}\n**\n'.format(InvNo, URL)
media.write(recordbuf)
InvNo = URL = None
Python flow control with Flag?