How to get listdir of a path with it path

Question:

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]
Asked By: Frost Dream

||

Answers:

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

Answered By: BDurand

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

import  os

for f in os.scandir('your_path'):
    print(f.path)
Answered By: RomanPerekhrest
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.