需求大概就是將b文件寫入a文件,然后再把a(bǔ)文件去重寫入c文件
不需要導(dǎo)入任何庫
想著放假無聊玩玩看吧~方法是Low了點纵穿,代碼是爛了點,但是吧
我樂意
def write():
a = open('a.txt', 'U').readlines()
b = open('b.txt', 'U').readlines()
# 要寫入的文件
ofile = open('a.txt', 'w')
# 遍歷讀取所有文件谓媒,并寫入到輸出文件
for txt in a:
ofile.write(txt)
ofile.write('\n')
for txt in b:
ofile.write(txt)
ofile.write('\n')
ofile.close()
def removal():
# 將a去重后寫入c
i = 0
read = "a.txt" # old
write = "c.txt" # new
lines_seen = set()
outfile = open(write, "w")
f = open(read, "r")
for line in f:
if line not in lines_seen:
i += 1
outfile.write(line)
lines_seen.add(line)
outfile.close()
f.close()
if __name__ == '__main__':
write()
removal()
有點餓了。句惯。先吃東西了土辩,畢竟
人生苦短