我正在尝试做一个函数来比较两个txt
文件。如果它识别出在一个文件中但不在另一个文件中的新行,则它会将它们添加到list
中,并且也将该文件添加到不包含这些新行的文件中。它没有这样做。这是我的功能。我究竟做错了什么?检查一个txt文件中是否有新字符串
newLinks = []
def newer():
with open('cnbcNewLinks.txt', 'w') as newL:
for line in open('cnbcCleanedLinks.txt'):
if line not in "cnbcNewLinks.txt":
newLinks.append(line)
newL.write(line)
else:
continue
cleaned = ''.join(newLinks)
print(cleaned)
'if line not in“cnbcNewLinks.txt”:'只是测试文件名的文本字符串,它是* not *搜索文件。 – cdarke
那么我如何搜索新字符串? –
使用文件句柄'newL',就像您在写入文件时所做的那样。 –