>>> os.path.realpath("C:")
'C:\\PythonApp'
>>> os.path.realpath("E:")
'E:\\'
我当前的目录是 C:\PythonApp。如果我给 os.listdir 之类的未知路径名,os 似乎默认为当前目录,那么为什么它适用于 E 而不是 C?
请您参考如下方法:
比较你得到的:
os.path.realpath("C:\\")
Windows 为每个驱动器保存一个“当前目录”,C:(没有斜杠)指的是那个,而不是驱动器的根目录。
os.path.join 的 python 文档提到了这一点:
Note that on Windows, since there is a current directory for each drive, os.path.join("c:", "foo") represents a path relative to the current directory on drive C: (c:foo), not c:\foo.
