pythondirectorylistdir

How to get listdir of a path with it path


i want this

>>> from os import listdir
>>> listdir(g:/new folder)
[g:/new folder/file 1, g:/new folder/file 2\]

but i'm getting this

>>> from os import listdir
>>> listdir(g:/new folder)
[file 1, file 2\]

Solution

  • Use os.scandir function to get full path for each entry:

    import  os
    
    for f in os.scandir('your_path'):
        print(f.path)