【Python】フォルダ内から特定の文字列を含むファイルの名前を抽出する方法
正規表現を用いたパターンマッチを行います.
files = os.listdir(os.getcwd()) files_file = [f for f in files if os.path.isfile(os.path.join(os.getcwd(), f))] pattern = r"(20180922).*(jpg)" #最初に20180922,最後にjpgのつくファイル名を正規表現で指定 for fn in files_file: if re.match(pattern , fn): print(fn) #条件に該当するファイル名をプリント