Sandip Bhattacharya wrote: > On Sat, Jul 30, 2011 at 10:28:11PM -0700, Richard D. Moores wrote: >> 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 > > The first backslash up there is escaping the ending quote. This is what > you want: > return path.replace('\\', '/') > > Generally, converting slashes manually should be kept at a minimum. You > should be using library functions as much as possible. The experts here > can correct me here, but this is a roundabout way I would be doing this: > > # I use a linux machine. Using this to work with Windows paths > # Use os.path if you are on windows > import ntpath > > # Use raw strings so that backslash doesnt matter > path=r'C:\Users\Dick\Desktop\Documents\Notes\College Notes.rtf' > > #take out drive first because ntpath.split end sentinel is predictable > #that way > drive,rest = ntpath.splitdrive(path) > > # This will store the path components > comps = [] > comps.append(drive) > > while rest != '\\': > parts = ntpath.split(rest) > comps.insert(1,parts[1]) > rest = parts[0] > > > print '/'.join(comps) > > I am not happy with the loop to collect the components. But I couldn't > find a single path function which splits a path into all the components > in one go.
What happens if the path looks like r"C:relative\path\to\my\file.txt" or r"C:/mixed\slashes.txt"? _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor