orig_string = "\\\\file_foo\\bar\\text-to-be-deleted\\foo-bar.pdf"
The original string needs to be modified (copy into a new variable) to look like the new_string below. The file has thousands of lines with the same format (file path of a pdf file).
new_string = "\\\\file_foo\\bar\\foo-bar.pdf"
How do I modify the orig_string to look like the new string?
Edit: Sorry, I forgot to mention on my original post. The '\text-to-be-deleted' is not the same. All filepath have different '\text-to-be-deleted' string.
Ex.
"\\\\file_foo\\bar\\path100\\foo-bar.pdf"
"\\\\file_foo\\bar\\path-sample\\foo-bar.pdf"
"\\\\file_foo\\bar\\another-text-be-deleted\\foo-bar.pdf"
... and so on.
I have a method. Hope it can help you.
orig_string = "\\\\file_foo\\bar\\text-to-be-deleted\\foo-bar.pdf"
back_index = orig_string.rfind('\\')
front_index = orig_string[:back_index].rfind('\\')
new_string = orig_string[:front_index] + orig_string[back_index:]
print(new_string)
Output
'\\\\file_foo\\bar\\foo-bar.pdf'
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments