count = {}
for w in open('Vimle_P_006_0_E1900.txt',errors='ignore',encoding='utf8').read().split():
if w in count:
count[w] += 1
else:
count[w] = 1
for word, times in count.items():
out_put = str(word) + " was found = " + str(times) + " times" + "\n"
with open('Test.txt','a',encoding='utf8') as f:
f.write(out_put)
f.close()
list_of_files
,您可以这样做:def write_to_files(infile):
count = {}
for w in open(infile,errors='ignore',encoding='utf8').read().split(): #open the file
if w in count:
count[w] += 1
else:
count[w] = 1
for word, times in count.items():
out_put = str(word) + " was found = " + str(times) + " times" + "\n"
with open(infile+"_out.txt",'a',encoding='utf8') as f: #write to a different file each time
f.write(out_put)
for file in list_of_files:
write_to_files(file) #iterate over the list of files and call the function for each file separately