os.join 创建了错误的路径

os.join creates the wrong path

我需要获取桌面上某个目录的绝对路径 我有:

 home = os.environ["HOMEPATH"]
 desktop =  os.path.join(home,'Desktop')
 name = 'Test_L1_R1
 fileName = os.path.abspath(os.path.join(desktop,r'\python35\connect4\pons\data',name))

结果应该是

C:\Users\Alan\Desktop\python35\connect4\pons\data\Test_L1_R1

但实际结果是

C:\python35\connect4\pons\data\Test_L1_R1

我相信 os 检测到我有一个目录

C:\python35

因为如果我将路径更改为

fileName = os.path.abspath(os.path.join(desktop,r'\connect4\pons\data',name))

即删除 python35 部分我得到正确答案

如何阻止它这样做?

来自the docs

If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.

停止在第二个参数中使用绝对路径。