我想從下面的循環將我的控制檯中顯示的結果打印到文本文件中。我試圖把這個代碼在循環中所看到的例子:將結果打印到文本文件
f = open('out.txt', 'w',)
sys.stdout = f
然而,當這是在環路我只得到一個結果集,而不是完整預期。
wordlist = input("What is your word list called?")
f = open(wordlist)
l = set(w.strip().lower() for w in f)
chatlog = input("What is your chat log called?")
with open(chatlog) as f:
found = False
for line in f:
line = line.lower()
if any(w in line for w in l):
print (l)
print(line)
found = True
f = open('out.txt', 'w',)
sys.stdout = f
if not found:
print("not here")