连接两个或者更多的路径
特别说明:- 如果各组件名首字母不包含’/’,则函数会自动加上
#!/usr/bin/env python # -*- encoding: utf-8 -*- ''' @Filename :os_path_join.py @Description : @Datatime :2021/11/16 21:56:58 @Author :qtxu @Version :v1.0 @Function :os.path.join() ''' import os path1 = "home" path2 = "qtxu" path3 = "20211116" path_joint1 = path1 + path2 +path3 path_joint2 = os.path.join(path1,path2,path3) print(path_joint1) print(path_joint2)结果1
(base) [qtxu@mu01 Pretrain_demo]$ python os_path_join.py homeqtxu20211116 home/qtxu/20211116示例2
import os path1 = "/home" path2 = "qtxu" path3 = "20211116" path_joint1 = path1 + path2 +path3 path_joint2 = os.path.join(path1,path2,path3) print(path_joint1) print(path_joint2)结果2
/homeqtxu20211116 /home/qtxu/20211116
- 如果有一个组件是一个绝对路径,则在它之前的所有组件均会被舍弃
import os path1 = "home" path2 = "/qtxu" ################ path3 = "20211116" path_joint1 = path1 + path2 +path3 path_joint2 = os.path.join(path1,path2,path3) print(path_joint1) print(path_joint2)结果1
home/qtxu20211116 /qtxu/20211116
- 如果最后一个组件为空,则生成的路径以一个’/’分隔符结尾
import os path1 = "home" path2 = "/qtxu" path3 = " " ################ path_joint1 = path1 + path2 +path3 path_joint2 = os.path.join(path1,path2,path3) print(path_joint1) print(path_joint2)结果
home/qtxu /qtxu/
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)