Well, to begin with, in tobestriped
you'll get an error as \'
will be escaped. You can use tobestriped = 'C:\\'
.
From this SO answer:
A "raw string literal" is a slightly different syntax for a string
literal, in which a backslash, \, is taken as meaning "just a
backslash" (except when it comes right before a quote that would
otherwise terminate the literal) -- no "escape sequences" to represent
newlines, tabs, backspaces, form-feeds, and so on. In normal string
literals, each backslash must be doubled up to avoid being taken as
the start of an escape sequence.
Next, In your paths list \f
will also be escaped. To get rid of this issue, make those strings raw strings:
filepaths = [r'C:\folder\file1.jpg', r'C:\file2.png', r'C:\file3.xls']
An you will have the desired result:
filepaths = [r'C:\folder\file1.jpg', r'C:\file2.png', r'C:\file3.xls']
tobestriped = 'C:\\'
for filepath in filepaths:
newfilepath = filepath.strip(tobestriped)
print(newfilepath)
Output:
folder\file1.jpg
file2.png
file3.xls
An alternative to your solution would be to take advantage of the fact that all your strings begin with C:\
so you can do something like this:
print([x[3:] for x in filepaths])