[Tutor] How to replace the '\'s in a path with '/'s?

Dharmit Shah shahdharmit at gmail.com
Sun Jul 31 08:09:45 CEST 2011


Ruchard,

Try return path.replace('\\', '/'). That gave me the output desired by you.
I don't know the reason. But I guess it's because \ is used as escape
character. I am sure someone in the list will point out the accurate reason.

On Sun, Jul 31, 2011 at 10:58 AM, Richard D. Moores <rdmoores at gmail.com>wrote:

> 64-bit Vista
> Python 3.2.1
>
> I would like to write a function that would take a path such as
> 'C:\Users\Dick\Desktop\Documents\Notes\College Notes.rtf'
> and return 'C:/Users/Dick/Desktop/Documents/Notes/College Notes.rtf' . I've
> tried this:
>
> def test(path):
>    return path.replace('\', '/')
>
> print(test('C:\Users\Dick\Desktop\Documents\Notes\College Notes.rtf'))
>
> gets me
>
> File "c:\P32Working\untitled-5.py", line 2
>    return path.replace('\', '/')
>                                ^
> SyntaxError: EOL while scanning string literal
> Process terminated with an exit code of 1
>
> Thanks,
>
> Dick
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Regards

Dharmit Shah <http://about.me/dharmit>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110731/aeb9bf34/attachment.html>


More information about the Tutor mailing list