Search code examples
pythonfilecountdirectorypathlib

How can I find the number of files within a directory?


How can I get the number of files in a certain folder using pathlib ?

from pathlib import Path
fileDir = Path("C:/Users/Jonas/Desktop/Test")

Solution

  • You could use .glob() or .rglob():

    from pathlib import Path
    fileDir = Path("C:/Users/Jonas/Desktop/Test")
    excelFiles = fileDir.rglob('*.xl*')
    print(len(list(excelFiles)))
    >>> 3