with open(abs_path, 'w') as new_file:
    with open(file_path) as old_file:
        for line in old_file:
            new_file.write(line.replace(pattern, subst))
Search and replace a line in a file in Python