Misunderstanding of python os.path.abspath

后端 未结 3 1946
梦如初夏
梦如初夏 2020-12-24 12:40

I have following code:

directory = r\'D:\\images\'
for file in os.listdir(directory):
    print(os.path.abspath(file))

and I want next outp

3条回答
  •  -上瘾入骨i
    2020-12-24 13:11

    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))
    

提交回复
热议问题