Home > other >  How to get listdir of a path with it path
How to get listdir of a path with it path

Time:12-26

from os import listdir

i want this

listdir(g:/new folder)
[g:/new folder/file 1, g:/new folder/file 2\]

but i'm getting this

listdir(g:/new folder)
[file 1, file 2\]

CodePudding user response:

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

import  os

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

CodePudding user response:

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

  • Related