if count == n_line:
    line = line.replace(str(line), text + '\n')
tmp.write(line)
shutil.copy(tmp_filename, filename)
os.remove(tmp_filename)

def create_test(fname):
    with open(fname, 'w') as f:
        f.write('1 \n2 \n3 \n4 \n5')
if __name__ == '__main__':
    create_test('writetest.txt')
writeline('writetest.txt', 4, 'This is the fourth line')
How to erase line from text file in Python?