有两个文件testfile1.txt和testfile2.txt,要求把这两个文件中的内容合并保存到一个新文件testfie3.txt中,并输出到屏幕
filename_3 = 'testfile3.txt'
with open('testfile1.txt') as filename_1:
contents_1 = filename_1.read()
with open('testfile2.txt') as filename_2:
contents_2 = filename_2.read()
contents = f"{contents_1}\n{contents_2}"
with open(filename_3,'w') as file_object:
file_object.write(contents)
try:
with open('testfile3.txt') as f:
contents_3 = f.read()
except FileNotFoundError:
print(f"Sorry,the file {filename_3} does not exist.")
else:
print(contents_3)
with open("testfile1.txt") as f:
con1=f.read()
with open("testfile2.txt") as f:
con2=f.read()
with open("testfile3.txt",'w') as f:
f.write(con1+'\n' + con2)
print(con1+'\n'+ con2)
这问题你也问!!!!