Check if a directory contains a file with a given extension

后端 未结 3 923
梦毁少年i
梦毁少年i 2020-12-29 21:58

I need to check the current directory and see if a file with an extension exists. My setup will (usually) only have one file with this extension. I need to check if that fil

3条回答
  •  一生所求
    2020-12-29 22:38

    You should use the glob module to look for exactly the files that you're interested in:

    import glob
    
    fileList = glob.glob("*.true")
    for trueFile in fileList:
        doSomethingWithFile(trueFile)
    

提交回复
热议问题