デスクトップのフォルダーから複数のテキスト ファイルを実行しようとしています。そこから olympic という単語を検索する必要があります。その 50 個のテキスト ファイルで olympic が見つかった場合は、 textfile1 = 2 textfile2 = 1 のように output.txt に保存する必要があります。 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
これは私がやろうとしていることですが、はい、エラーがいっぱいあることを知っています:)
複数のファイルを実行しようとしていますが、手動ではありません。ディレクトリ/フォルダー全体のファイルを自動的に実行し、それらの出力を 1 つのテキスト ファイルに保存する必要があります..'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