作用: 连接两个或更多的路径名组件
如果各组件名首字母不包含’/’,则函数会自动加上 Python 2.7.12 (default, Jul 21 2020, 15:19:50) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> p1 = 'a' >>> p2 = 'b' >>> p3 = 'c' >>> p1 + p2 +p3 'abc' >>> os.path.join(p1, p2, p3) 'a/b/c' 如果有一个组件是一个绝对路径,则在它之前的所有组件均会被舍弃(通俗点说os.path.join() 适配以‘/’开始之后的内容 >>> import os >>> p1 = '/a' >>> p2 = 'b' >>> p3 = 'c' >>> p1 + p2 +p3 '/abc' >>> os.path.join(p1, p2, p3) '/a/b/c' >>> os.path.join(p2, p1, p3) '/a/c' >>> os.path.join(p3, p2, p1) '/a' 如果最后一个组件为空,则生成的路径以一个’/'分隔符结尾 >>> import os >>> p1 = '/a' >>> p2 = 'b' >>> p3 = '' >>> p1 + p2 +p3 '/ab' >>> os.path.join(p1, p2, p3) '/a/b/' >>> os.path.join(p1, p3, p2) '/a/b' >>> os.path.join(p2, p1, p3) '/a/'