python get file in a folder of all the specified suffix

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 separation
  • os.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)
He published 191 original articles · won praise 104 · views 340 000 +

Guess you like

Origin blog.csdn.net/u013925378/article/details/102742487