我有三个不同的.txt文件,在每个文件中,我想添加一个不同的标头。例如:line1="rain", add line to file 1;line2="cap", add line to file 2;line3="different", add line to file 3;
我尝试编写一个函数writing_to_files
,该函数可以简化操作,但是我对如何添加行的内容有疑问。
def writing_to_files(filename, header_names):
'''recovers file and writes a line in the file'''
with file(filename, 'r') as original:
data=original.read()
with file(filename, 'w') as modified:
modified.write(header_names + "\n" + data)
return True