1
私はPythonの新機能で、次の問題を解決しようとしています。単語を含むリストを繰り返し、ファイル内で検索し、見つかった場合は単語が見つかった行番号を返します。Python - リスト内の値からファイルを検索する
files = glob.iglob(os.path.join(in_dir, '*.*'))
List = ['word1', 'word2',...'wordn']
for index, word in enumerate(List):
print(index, word)
for item in files: # Iterate all files in Input directory
filename = os.path.basename(item) # Copy the filename without the path
print('file: {0}'.format(filename))
with open(item, 'r') as f:
for num, line in enumerate(f, 0):
if word in line:
print('num: {0}, line: {1}'.format(num, line))
しかし、それは動作していないようで、リストの最初の値のみを検索しました。私がしたいのは、すべてのファイルのすべてのList値を検索することです。
OMG !!! @たくさんのことをありがとう!!! – Maki