python get file in a folder under all specified suffix. In a folder, the file may contain several types of data, such as .png, .txt, .json, sometimes only need to get certain types of files which are processed.
related functions:
os.path.splitext()
: File name and extension separationos.listdir()
: Find all files from this category
Code:
import os datanames = os.listdir (train_dir) for DataName in datanames: if os.path.splitext (dataname) [1] == '.json': # directory contains the files .json print (dataname)