Python: os.listdir alternative/certain extensions

后端 未结 5 1455
梦如初夏
梦如初夏 2021-01-05 05:15

Is it possible to see files with certain extensions with the os.listdir command? I want it to work so it may show only files or folders with .f at the end. I checked the doc

5条回答
  •  孤街浪徒
    2021-01-05 05:55

    Try this:

    from os import listdir
    
    extension = '.wantedExtension'
    
    mypath = r'my\path'
    
    filesWithExtension = [ f for f in listdir(mypath) if f[(len(f) - len(extension)):len(f)].find(extension)>=0 ]
    

提交回复
热议问题