Search code examples
pythonglob

list of name and path of files in a directory and subdirectory where one or more subdir have a point in the middle python


I'm trying to get path and filename, from a directory, including those ones inside the subdirectories. The problem is that some subfolders have one or more points in the name.
So when I execute this code

listaFile=glob.glob('c:\test\ID_1'+/**/*.*',recursive= True)

I get
c:\test\ID_1\fil.e1.txt
c:\test\ID_1\fil.e2.doc
c:\test\ID_1\subfolder1\file1.txt
c:\test\ID_1\sub.folder2 (instead of c:\test\ID_1\sub.folder2\file1.txt)
thank you all in advance!


Solution

  • You need to filter it out checking if it's a file or folder. An easy way would be to use the pathlib instead of glob directly. Example below.

    listaFile = [str(path) for path in pathlib.Path(r"c:\test\ID_1").rglob("*.*") if path.is_file()]