1. 程式人生 > >python3-對某目錄下的文本文件分詞

python3-對某目錄下的文本文件分詞

dynamic rom help any end eal txt orm script

from pathlib import Path import os import re pathName=‘./‘ fnLst=list(filter(lambda x:not x.is_dir(),Path(pathName).glob(‘**/*.txt‘))) print(fnLst) for fn in fnLst: with open(fn) as f: print() print(fn) for line in f: for word in re.findall(r‘\w+‘, line): print(word,end="|")

輸出結果為:

[PosixPath(‘2.txt‘), PosixPath(‘1.txt‘)]

2.txt
This|tutorial|introduces|the|reader|informally|to|the|basic|concepts|and|features|of|the|Python|language|and|system|It|helps|to|have|a|Python|interpreter|handy|for|hands|on|experience|but|all|examples|are|self|contained|so|the|tutorial|can|be|read|off|line|as|well|
1.txt
Python|is|an|easy|to|learn|powerful|programming|language|It|has|efficient|high|level|data|structures|and|a|simple|but|effective|approach|to|object|oriented|programming|Python|s|elegant|syntax|and|dynamic|typing|together|with|its|interpreted|nature|make|it|an|ideal|language|for|scripting|and|rapid|application|development|in|many|areas|on|most|platforms|

python3-對某目錄下的文本文件分詞