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))
我如何使此代码追加到特定行
答案 0 :(得分:0)
如果您想在现有文件中插入新行,则实际上必须重建该文件(至少从该点开始)。 这是因为从程序上来说,它下面的所有行都是不同的。
我要做的是将整个文件读入列表,然后将要创建的行合并到所述列表中,然后将输出写入新文件。