python中将Windows的路径转换成Linux路径

今天遇到一个问题,在集成的环境中是Linux,本地开发是Windows,在帮助文档的编写中,路径超链接表达有问题,写了一个小函数解决这个问题,

def windows_to_linux(windowsPath):
    windowsPath_h = windowsPath.split(':')[1] #将windows中以:分割成两个部分
    windowsPath_w = windowsPath_h.split("\\")#将后面部分以\\分割,会得到一个列表
    del windowsPath_w[0]#删除第一个元素
    linuxPath1 = "/".join(windowsPath_w)#以/连接
    hh = windowsPath.split(':')[0]#用一个变量接受头部
    hh = '/' + hh + "/" #在头部前后各加一个/
    linuxPath = hh + linuxPath1 
    print(linuxPath)

输入:

w1 = "D:\whyuandashuai\syslab-docs\sys\projects\Help\Doc\Tydashuai\Regression\partialDependence_py.md"
windows_to_linux(w1)

输出:

/D/whyuandashuai/syslab-docs/sys/projects/Help/Doc/Tydashuai/Regression/partialDependence_py.md

大功告成。

觉得有用的道友自行取用!

2022.11.23_悍马提缰

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值