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

前端 未结 10 1973
暖寄归人
暖寄归人 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

    'files' already tells you whats in the directory. Just check it:

    for dirpath, dirnames, files in os.walk('.'):
        if files:
            print(dirpath, 'has files')
        if not files:
            print(dirpath, 'is empty')
    

提交回复
热议问题