How to check to see if a folder contains files using python 3

前端 未结 10 1918
暖寄归人
暖寄归人 2020-12-01 11:40

I\'ve searched everywhere for this answer but can\'t find it.

I\'m trying to come up with a script that will search for a particular subfolder then check if it conta

10条回答
  •  粉色の甜心
    2020-12-01 12:36

    You can use this simple code:

    dir_contents = [x for x in os.listdir('.') if not x.startswith('.')]
    if len(dir_contents) > 0:
        print("Directory contains files")
    

    It checks for files and directories in the current working directory (.). You can change . in os.listdir() to check any other directory.

提交回复
热议问题