Python篩選目錄下指定後綴的文件

Python篩選目錄下指定後綴的文件

 

From: https://stackoverflow.com/questions/2225564/get-a-filtered-list-of-files-in-a-directory

Method one:

glob.glob('145592*.jpg')

glob.glob() is definitely the way to do it (as per Ignacio). However, if you do need more complicated matching, you can do it with a list comprehension and re.match(), something like so:

files = [f for f in os.listdir('.') if re.match(r'[0-9]+.*\.jpg', f)]

篩選出包含'jpg','jpeg', 'bmp', 'png', 'gif'

import os

relevant_path = "[path to folder]"

included_extensions = ['jpg','jpeg', 'bmp', 'png', 'gif']

file_names = [fn for fn in os.listdir(relevant_path)
              if any(fn.endswith(ext) for ext in included_extensions)]

篩選出包含'jpg',

import os, fnmatch

fnmatch.filter(os.listdir('.'), '*.py')
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章