在python中将反斜杠转换为正斜杠 [英] Convert backward slash to forward slash in python

查看:2181
本文介绍了在python中将反斜杠转换为正斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我已经阅读了有关将反斜杠转换为正斜杠的文章. 但是sol是使用原始字符串.

Hi I have read articles related converting backward to forward slashes. But sol was to use raw string.

但是我的问题是:

我将动态获取文件路径到变量 var ='C:\ dummy_folder \ a.txt' 在这种情况下,我需要将其转换为正斜杠. 但是由于'\ a',我无法转换为正斜杠

I will get file path dynamically to a variable var='C:\dummy_folder\a.txt' In this case i need to convert it to Forward slashes. But due to '\a',i am not able to convert to forward slashes

我如何转换它?或者我应该如何将此字符串更改为原始字符串,以便可以将其更改为正斜杠

How to i convert it? OR How should i change this string to raw string so that i can change it to forward slash

推荐答案

请勿执行此操作.只需使用 os.path 并让它处理所有事情.您不应明确设置正斜杠或反斜杠.

Don't do this. Just use os.path and let it handle everything. You should not explicitly set the forward or backward slashes.

>>> var=r'C:\dummy_folder\a.txt'
>>> var.replace('\\', '/')
'C:/dummy_folder/a.txt'

但同样,不要.只需使用os.path即可开心!

But again, don't. Just use os.path and be happy!

这篇关于在python中将反斜杠转换为正斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆