私はキーワードのリストを持っており、キーワードを含むファイル名を探しているディレクトリを調べたいと思っています。見つかった場合は、一致するキーワードとファイルパスを辞書にバインドします。filenameにリスト内の単語の部分文字列があるかどうかをチェックする
keywords = ['mon', 'tue', 'wed']
dict = {}
directory = os.fsencode(r"my_dir")
for file in os.listdir(directory):
filename = os.fsdecode(file)
ext = Path(file).suffix
if filename in keywords:
filepath = os.path.join(directory, filename, ext)
dict[keyword] = filepath
だから、最後に、私はこのような何かしたい:
{'mon': 'F:\mon_001.txt', 'tue': 'F:\tue_999.txt', 'wed': 'F\wed_123.txt'}
を今どのように私は試合を見つけるのですか?
"if keywords in filename"を使用しないでください。 – SuperStew