我正在尝试从桌面上的文件夹中运行多个文本文件,我必须从中搜索单词 olympic,如果它在这 50 个文本文件中找到 olympic,则应该将其保存在 output.txt 中,例如 textfile1 = 2 textfile2 = 1 和依此类推,直到 texfile=50
import glob
import re
write_file = open("output.txt")
flist = glob.glob("./*.py") # adjust glob pattern as desired
print flist
print " lines : path to file"
for fpath in flist:
with open(fpath) as f: #open files
lines = f.readlines()
if (lines == 'olympic'):
write_file.write("%s" % lines) #write the results
print "%6d : %s" % (len(lines),fpath)
#with open securely opens and closes the file
write_file.close() # close file
这就是我想要做的,但是是的,我知道它充满了错误:)
我正在尝试运行多个文件,但不是手动运行,我希望它自动运行整个目录/文件夹的文件并将它们的输出保存在一个文本文件中..'I have 50 text files and all files have word olympic , some have 1 some have 2/3 etc , i want to count the words from each text file and than save their output in one text file , like textfile1 = 2 , textfile2 = 3 etc in output.txt