为什么 os.path.isabs() 没有给出我期望的结果

问题描述 投票:0回答:2
print(str(os.path.isabs("c:///\\\\xmls\\hello.txt")))

此源代码返回

True

这是为什么?

c:///\\\\xmls\\hello.txt
怎么可能是有效路径?

python path
2个回答
2
投票

来自官方文档:

Return True if path is an absolute pathname. 
On Unix, that means it begins with a slash, 
on Windows that it begins with a (back)slash after chopping off a potential drive letter.

换句话说,即使是这个代码也会打印

True

os.path.isabs("/hello world")

因为它以

/

开头

-1
投票

pycharm 在 python 控制台中将“/Users/name/folder”显示为 True。当在 python 中作为脚本运行时,它会产生 False —— 东西坏了?

Mac OSX

© www.soinside.com 2019 - 2024. All rights reserved.