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]
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
Use os.scandir
function to get full path for each entry:
import os
for f in os.scandir('your_path'):
print(f.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]
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
Use os.scandir
function to get full path for each entry:
import os
for f in os.scandir('your_path'):
print(f.path)