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

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

    With pathlib this can be done as follows:

    import pathlib
    
    # helper function
    def is_empty(_dir: pathlib.PAth) -> bool:
        return not bool([_ for _ in _dir.iterdir()])
    
    # create empty dir
    _dir = pathlib.Path("abc")
    
    # check if dir empty
    is_empty(_dir)  # will retuen True
    
    # add file s to folder and call it again
    
    
    

提交回复
热议问题