Search code examples
pythonexcelsubdirectory

List files in specified directory without subdirectories


Below code goes thru all excel files including files in sub-folders, how can i only go thru files without files in subfolders. thanks

dirpath=Path(pl.PureWindowsPath(values['-FIN-']))
for path in dirpath.rglob("*.xls*"):
                           

Solution

  • Use the glob method instead of rglob also we gonna use is_file() method to checks if the path is a file and not a directory.

    from pathlib import Path
    import os
    
    dirpath = Path(values['-FIN-'])
    for path in dirpath.glob("*.xls*"):
        if path.is_file():
            print(path)