I have following code:
directory = r\'D:\\images\'
for file in os.listdir(directory):
print(os.path.abspath(file))
and I want next outp
listdir
produces the file names in a directory, with no reference to the name of the directory itself. Without any other information, abspath
can only form an absolute path from the only directory it can know about: the current working directory. You can always change the working directory before your loop:
os.chdir(directory)
for f in os.listdir('.'):
print(os.path.abspath(f))