【Python3笔记】关于os.path.isabs()

2018-08-21 05:42:48来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

先来看几个使用os,path.isabs()的例子:

很显然,第一个路径是没有问题,而后面都是我随便输入的一串字符,为什么有的返回True有的返回False呢?

 

我查看了Python3.5的文档,发现了这样一句话:

os.path.isabs(path)

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.

也就是说在WIndow系统下,如果输入的字符串以" / "开头,os.path.isabs()就会返回True,那么第四个例子就可以理解了。

 

而第二个和第三个有什么区别呢?不是都应该返回False吗?

查阅资料后我找到了下面这段话:

The current os.path.isabs documentation says:
> isabs(path) 
>    Return True if path is an absolute pathname (begins with a slash). 
The "begins with a slash" part is incorrect since certain systems use a
different pathname notation.
For example, on Macintosh (where os.sep == ":") this is an absolute
pathname:
hardDriveName:folderName1:folderName2:fileName.ext
...and this is a relative one:
:folderName1:fileName.ext
Moreover, on Windows os.path.isabs('\\') returns True since '\\' is an
alias for the current drive letter (e.g. C:\\) hence, independently from
what said before, the documentation should include also the "backslash"
term.
It turns out that on Windows there are really 4 different kinds of paths:
1) Completely relative, e.g. foo\bar
2) Completely absolute, e.g. c:\foo\bar or \\server\share
3) Halfbreeds with no drive, e.g. \foo\bar
4) Halfbreeds relative to the current working directory on a specific drive, e.g. c:foo\bar
Python 2.5's os.path.isabs() method considers both (2) and (3) to be absolute;

虽然是python2的,但是这和python3应该没什么区别。从上面这段话可以知道,在Windows系统上,以“  //  "开头的字符串,os.path.isabs也会返回True

然后我们又可以知道:

这就能解释为什么第二个返回False而第三个返回True了。

 

 

 

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:常用模块random,time,os,sys

下一篇:初遇python进程