【发布时间】:2021-03-05 04:53:36
【问题描述】:
有没有办法将file1的内容附加到file2的开头?我试过用这个方法,但它似乎不起作用
def main():
"""The program does the following:
- it inserts all the content from file2.txt to the beginning of file1.txt
Note: After execution of the your program, only file1.txt is updated, file2.txt does not change."""
#WRITE YOUR PROGRAM HERE
#Call the main() function
if __name__ == '__main__':
main()
f = open('file1.txt', 'r')
f1 = open('file2.txt', 'r')
def insert(file2, string):
with open('file2.txt','r') as f:
with open('file1.txt','w') as f1:
f1.write(string)
f1.write(f.read())
os.rename('file1.txt',file2)
# closing the files
f.close()
f1.close()
【问题讨论】:
-
这能回答你的问题吗? Prepend line to beginning of a file