The method os.listdir()
returns a list containing the names of the found files and directories, what may be happening is that you must be using the method os.path.abspath()
pointing to the returned list of os.listdir()
wrongly, by applying os.path.abspath()
for each item on the list should work.
import os
dirlist = os.listdir(".")
for i in dirlist:
filename = os.path.abspath(i)
print(filename)
The exit must be something like this:
/home/user/dir1/dir2/arquivo1.py
/home/user/dir1/dir2/arquivo2.py
/home/user/dir1/dir2/foobarDir1
/home/user/dir1/dir2/foobarDir2
Updating
As an alternative to the method os.walk()
we could be doing something like:
import os
filedirlist = os.listdir(".")
filelist = [os.path.abspath(f) for f in filedirlist if os.path.isfile(f)]
dirlist = [os.path.abspath(d) for d in filedirlist if os.path.isdir(d)]
# Lista de arquivos com path completo.
for i in filelist:
print(i)
# Lista de diretórios com path completo.
for i in dirlist:
print(i)
I solved my problem using the string I used in the listdir() + method, but still, it would be good to know what the problem of the os.path.abspath method is.()
– rogger