二、假设桌面有五个文件:data1.txt,data2.txt,…data5.txt(可手动创建),每个文件分别记录了一些内容(自定),编写程序,生成一个data.txt文件,内容为五个文件的所有内容合并(每个文件的内容之间用换行隔开)。
import os
if not os.path.exists("C:/Users/DELL/Desktop/data.txt"):
f = open("C:/Users/DELL/Desktop/data.txt",'w')
f.close()
f = open("C:/Users/DELL/Desktop/data.txt",'r')
a = f.read()
f.close()
k = open("C:/Users/DELL/Desktop/data1.txt",'r')
b = k.read()
k.close()
h = open("C:/Users/DELL/Desktop/data2.txt",'r')
c = h.read()
h.close()
p = open("C:/Users/DELL/Desktop/data3.txt",'r')
j = p.read()
p.close()
g = open("C:/Users/DELL/Desktop/data4.txt",'r')
d = g.read()
g.close()
l = open("C:/Users/DELL/Desktop/date5.1.txt",'r')
e = l.read()
l.close()
h = '\n'
a+= b + h + h
a+= c + h + h
a+= j + h + h
a+= d + h + h
a+= e + h + h
f = open("C:/Users/DELL/Desktop/data.txt",'w')
f.write(a)
f.close()
就是这样哦!和换行换了两次防止看走眼