Check if a directory contains a file with a given extension

后端 未结 3 936
梦毁少年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

    If you only want to check that any file ends with a particular extension, use any.

    import os
    if any(File.endswith(".true") for File in os.listdir(".")):
        print("true")
    else:
        print("false")
    

提交回复
热议问题