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

Richard D. Moores rdmoores at gmail.com
Sun Jul 31 18:18:33 CEST 2011


On Sun, Jul 31, 2011 at 08:59, Sergey <sergey at mighty.co.za> wrote:
> Gotcha!
> http://pymon.googlecode.com/svn/tags/pymon-0.2/Internet/rsync.py
> 231-239 strings
>
> ## code ##
>
> def convertPath(path):
>        # Convert windows, mac path to unix version.
>        separator = os.path.normpath("/")
>        if separator != "/":
>                path = re.sub(re.escape(separator), "/", path)
>
>        # Help file, folder pattern to express that it should match the all file or
> folder name.
>        path = "/" + path
>        return path
>
> ## code ##

Nice!

I went with


import os.path
import re

def convertPath(path):
    separator = os.path.normpath("/")
    if separator != "/":
        path = re.sub(re.escape(separator), "/", path)

    return path



path=r'C:\Users\Dick\Desktop\Documents\Notes\College Notes.rtf'

print(convertPath(path))

"""
Output
C:/Users/Dick/Desktop/Documents/Notes/College Notes.rtf
"""
Dick


More information about the Tutor mailing list