Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace multiple forward slashes in a directory by a single slash?

My path:

'/home//user////document/test.jpg'

I want this to be converted into:

'/home/user/document/test.jpg'

How to do this?

like image 370
Dilu Vinayan Avatar asked Oct 21 '20 08:10

Dilu Vinayan


People also ask

How do you change a forward slash?

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.

What does double slash mean in directory?

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.

What is the difference between forward slashes and backslashes?

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.

How do you change backward slash to forward slash?

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.


1 Answers

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'
like image 105
deceze Avatar answered Sep 25 '22 10:09

deceze