python os.path.join()

python os.path.join(),第1张

python os.path.join() python os.path.join() 一、 函数功能

连接两个或者更多的路径

特别说明:
  1. 如果各组件名首字母不包含’/’,则函数会自动加上
示例1
#!/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
  1. 如果有一个组件是一个绝对路径,则在它之前的所有组件均会被舍弃
示例1
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
  1. 如果最后一个组件为空,则生成的路径以一个’/’分隔符结尾
示例1
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/ 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5521586.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存