python路径拼接os.path.join()函数的用法

转载文章
os.path.join()函数:连接两个或更多的路径名组件

1.如果各组件名首字母不包含’/’,则函数会自动加上

2.如果有一个组件是一个绝对路径,则在它之前的所有组件均会被舍弃

3.如果最后一个组件为空,则生成的路径以一个’/’分隔符结尾

Demo1
import os

Path1 = ‘home’
Path2 = ‘develop’
Path3 = ‘code’

Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1,Path2,Path3)
print ('Path10 = ',Path10)
print ('Path20 = ',Path20)

输出

Path10 = homedevelopcode
Path20 = home\develop\code


Demo2

import os

Path1 = ‘/home’
Path2 = ‘develop’
Path3 = ‘code’

Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1,Path2,Path3)
print ('Path10 = ',Path10)
print ('Path20 = ',Path20)
输出

Path10 = /homedevelopcode
Path20 = /home\develop\code


Demo3
import os

Path1 = ‘home’
Path2 = ‘/develop’
Path3 = ‘code’

Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1,Path2,Path3)
print ('Path10 = ',Path10)
print ('Path20 = ',Path20)

输出

Path10 = home/developcode
Path20 = /develop\code


Demo4
import os

Path1 = ‘home’
Path2 = ‘develop’
Path3 = ‘/code’

Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1,Path2,Path3)
print ('Path10 = ',Path10)
print ('Path20 = ',Path20 )

输出

Path10 = homedevelop/code
Path20 = /code

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值