将文本附加到文本文件中的某个位置

时间:2018-08-28 12:06:06

标签: python python-3.x

def append_csv_file(filename):
    with open (filename, "a") as csv_file:
        file_name = csv_file
        appending = input("please enter the number of times you would like to enter "
                          "some text into the file on a different line: ")
        appending = int(appending)
        for appending in range(appending):
            x = input("please enter what you would like to append: ")
            file_name.write("\n" + x)

        exit()


xxx = input("please enter the file you would like to append to the file: ")
print(append_csv_file(xxx))

我如何使此代码追加到特定行

1 个答案:

答案 0 :(得分:0)

如果您想在现有文件中插入新行,则实际上必须重建该文件(至少从该点开始)。 这是因为从程序上来说,它下面的所有行都是不同的。

我要做的是将整个文件读入列表,然后将要创建的行合并到所述列表中,然后将输出写入新文件。