My path:
'/home//user////document/test.jpg'
I want this to be converted into:
'/home/user/document/test.jpg'
How to do this?
To replace all forward slashes in a string: Call the replaceAll() method, passing it a string containing a forward slash as the first parameter and the replacement string as the second. The replaceAll method returns a new string with all of the matches replaced.
Actually it means nothing and is ignored. This often happens when output from multiple places is combined and it isn't clear who's job it is to add the slashes, so both parties do it and you end up with two of them. Semantically in the case of a directory path is has no meaning and will be ignored by most programs.
Summary: The Backslash and Forward Slash The backslash (\) is mostly used in computing and isn't a punctuation mark. The forward slash (/) can be used in place of “or” in less formal writing. It's also used to write dates, fractions, abbreviations, and URLs.
By default the <Leader> key is backslash, and <Bslash> is a way to refer to a backslash in a mapping, so by default these commands map \/ and \\ respectively. Press \/ to change every backslash to a forward slash, in the current line. Press \\ to change every forward slash to a backslash, in the current line.
Use os.path.abspath
or normpath
to canonicalise the path:
>>> import os.path
>>> os.path.abspath('/home//user////document/test.jpg')
'/home/user/document/test.jpg'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With