How to get listdir of a path with it path

287 Views Asked by At

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\]
2

There are 2 best solutions below

0
RomanPerekhrest On BEST ANSWER

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

import  os

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

directory = "g:/new folder"
files = os.listdir(directory)
files_with_path = [directory+"/"+filename for filename in files]

It should do the trick given the doc, you could also look at the glob package https://docs.python.org/3/library/os.html#os.listdir

https://docs.python.org/3/library/glob.html