The os.listdir()
method returns a list containing the names of the files and directories found, what might be happening is that you should be using the os.path.abspath()
pointing to the returned list of os.listdir()
in the wrong way, applying os.path.abspath()
to each list item should work.
import os
dirlist = os.listdir(".")
for i in dirlist:
filename = os.path.abspath(i)
print(filename)
The output should look 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
Update
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)