with open('filename', 'r+') as f:
    while True:
        line = f.readline()
if line == '':
    break
readLoc = f.tell()
if isRequired(line):
    f.seek(writeLoc)
f.write(line)
writeLoc = f.tell()
f.seek(readLoc)
f.truncate(writeLoc)
How do I remove lines from a big file in Python, within limited environment