Python os.path.abspath的误解
Python os.path.abspath的误解
我有以下代码:
directory = r'D:\images' for file in os.listdir(directory): print(os.path.abspath(file))
我想要以下输出:
- D:\\images\\img1.jpg
- D:\\images\\img2.jpg以此类推
但我得到了不同的结果:
- D:\\code\\img1.jpg
- D:\\code\\img2.jpg
其中D:\\code是我的当前工作目录,结果与
os.path.normpath(os.path.join(os.getcwd(), file))
相同。所以,问题是:
os.path.abspath的目的是什么,为什么我必须使用
os.path.normpath(os.path.join(directory, file))
来获取我的文件的实际绝对路径?如果可能,请展示真实的用例。
admin 更改状态以发布 2023年5月23日