我在同一個文件夾中有10個文本文件。我想在for循環中分別處理每個文本文件。Python中循環的文本文件
def qwerty():
with open('file.txt') as f:
dicts = {}
for words in f:
split_words = reg.finditer(words)
for line in split_words:
group1 = line.group(1)
if group1 not in dicts:
dicts[group1] = 1
else:
dicts[group1] += 1
return dicts
這是我的代碼來處理單個文本文件。我如何運行一個循環來處理我所有的文本文件?所以文本文件的數量對應於for循環中的迭代次數。
難道這些文本文件名遵循一個模式?你可以從'glob import glob'中輸入''然後'在glob中輸入文件名(「* .txt」)'。 – tdelaney
@tdelaney嗨,沒有文本文件不遵循名稱模式 –
是否要處理該目錄中的所有文件? – tdelaney