假设当前文件词,存于文件A.txt,内容为:
aa
bb
cc
dd
ff
ee
gg
假设特定文件词,存于文件B.txt,内容为:
aa
bb
实现如下:
path = 'A.txt'
path_word = 'B.txt'
# 删除结果存在C.txt中
path_res = 'C.txt'
source = []
with open(path, 'r') as scan_file:
for line in scan_file:
source.append(line.strip())
print(source)
print(len(source))
with open(path_word, 'r') as scan_file:
for line in scan_file:
if (source.count(line.strip()) != 0):
source.remove(line.strip())
with open(path_res, 'w') as output_file:
for i in range(len(source)):
str_tmp = source[i] + "\n"
output_file.write(str_tmp)