python replace single backslash with double backslash
Question:
In python, I am trying to replace a single backslash (“”) with a double backslash(“”). I have the following code:
directory = string.replace("C:UsersJoshDesktop20130216", "", "\")
However, this gives an error message saying it doesn’t like the double backslash. Can anyone help?
Answers:
Use escape characters: "full\path\here"
, "\"
and "\\"
Use:
string.replace(r"C:UsersJoshDesktop20130216", "\", "\")
Escape the
character.
No need to use str.replace
or string.replace
here, just convert that string to a raw string:
>>> strs = r"C:UsersJoshDesktop20130216"
^
|
notice the 'r'
Below is the repr
version of the above string, that’s why you’re seeing \
here.
But, in fact the actual string contains just ''
not \
.
>>> strs
'C:\Users\Josh\Desktop\20130216'
>>> s = r"fo"
>>> s #repr representation
'f\o'
>>> len(s) #length is 3, as there's only one `''`
3
But when you’re going to print this string you’ll not get '\'
in the output.
>>> print strs
C:UsersJoshDesktop20130216
If you want the string to show '\'
during print
then use str.replace
:
>>> new_strs = strs.replace('\','\\')
>>> print new_strs
C:\Users\Josh\Desktop\20130216
repr
version will now show \\
:
>>> new_strs
'C:\\Users\\Josh\\Desktop\\20130216'
Maybe a syntax error in your case,
you may change the line to:
directory = str(r"C:UsersJoshDesktop20130216").replace('\','\\')
which give you the right following output:
C:\Users\Josh\Desktop\20130216
Given the source string, manipulation with os.path might make more sense, but here’s a string solution;
>>> s=r"C:UsersJoshDesktop\20130216"
>>> '\\'.join(filter(bool, s.split('\')))
'C:\\Users\\Josh\\Desktop\\20130216'
Note that split
treats the \
in the source string as a delimited empty string. Using filter
gets rid of those empty strings so join
won’t double the already doubled backslashes. Unfortunately, if you have 3 or more, they get reduced to doubled backslashes, but I don’t think that hurts you in a windows path expression.
The backslash indicates a special escape character. Therefore, directory = path_to_directory.replace("", "\")
would cause Python to think that the first argument to replace didn’t end until the starting quotation of the second argument since it understood the ending quotation as an escape character.
directory=path_to_directory.replace("\","\\")
Let me make it simple and clear. Lets use the re module in python to escape the special characters.
Python script :
import re
s = "C:UsersJoshDesktop"
print s
print re.escape(s)
Output :
C:UsersJoshDesktop
C:\Users\Josh\Desktop
Explanation :
Now observe that re.escape function on escaping the special chars in the given string we able to add an other backslash before each backslash, and finally the output results in a double backslash, the desired output.
Hope this helps you.
In python
(backslash) is used as an escape character. What this means that in places where you wish to insert a special character (such as newline), you would use the backslash and another character (n
for newline)
With your example string you would notice that when you put "C:UsersJoshDesktop20130216"
in the repl you will get "C:\Users\Josh\Desktopx8130216"
. This is because 2
has a special meaning in a python string. If you wish to specify
then you need to put two \
in your string.
"C:\Users\Josh\Desktop\28130216"
The other option is to notify python that your entire string must NOT use
as an escape character by pre-pending the string with r
r"C:UsersJoshDesktop20130216"
This is a “raw” string, and very useful in situations where you need to use lots of backslashes such as with regular expression strings.
In case you still wish to replace that single
with \
you would then use:
directory = string.replace(r"C:UsersJoshDesktop20130216", "\", "\\")
Notice that I am not using r'
in the last two strings above. This is because, when you use the r'
form of strings you cannot end that string with a single
Why can't Python's raw string literals end with a single backslash?
You could use
os.path.abspath(path_with_backlash)
it returns the path with
In python, I am trying to replace a single backslash (“”) with a double backslash(“”). I have the following code:
directory = string.replace("C:UsersJoshDesktop20130216", "", "\")
However, this gives an error message saying it doesn’t like the double backslash. Can anyone help?
Use escape characters: "full\path\here"
, "\"
and "\\"
Use:
string.replace(r"C:UsersJoshDesktop20130216", "\", "\")
Escape the character.
No need to use str.replace
or string.replace
here, just convert that string to a raw string:
>>> strs = r"C:UsersJoshDesktop20130216"
^
|
notice the 'r'
Below is the repr
version of the above string, that’s why you’re seeing \
here.
But, in fact the actual string contains just ''
not \
.
>>> strs
'C:\Users\Josh\Desktop\20130216'
>>> s = r"fo"
>>> s #repr representation
'f\o'
>>> len(s) #length is 3, as there's only one `''`
3
But when you’re going to print this string you’ll not get '\'
in the output.
>>> print strs
C:UsersJoshDesktop20130216
If you want the string to show '\'
during print
then use str.replace
:
>>> new_strs = strs.replace('\','\\')
>>> print new_strs
C:\Users\Josh\Desktop\20130216
repr
version will now show \\
:
>>> new_strs
'C:\\Users\\Josh\\Desktop\\20130216'
Maybe a syntax error in your case,
you may change the line to:
directory = str(r"C:UsersJoshDesktop20130216").replace('\','\\')
which give you the right following output:
C:\Users\Josh\Desktop\20130216
Given the source string, manipulation with os.path might make more sense, but here’s a string solution;
>>> s=r"C:UsersJoshDesktop\20130216"
>>> '\\'.join(filter(bool, s.split('\')))
'C:\\Users\\Josh\\Desktop\\20130216'
Note that split
treats the \
in the source string as a delimited empty string. Using filter
gets rid of those empty strings so join
won’t double the already doubled backslashes. Unfortunately, if you have 3 or more, they get reduced to doubled backslashes, but I don’t think that hurts you in a windows path expression.
The backslash indicates a special escape character. Therefore, directory = path_to_directory.replace("", "\")
would cause Python to think that the first argument to replace didn’t end until the starting quotation of the second argument since it understood the ending quotation as an escape character.
directory=path_to_directory.replace("\","\\")
Let me make it simple and clear. Lets use the re module in python to escape the special characters.
Python script :
import re
s = "C:UsersJoshDesktop"
print s
print re.escape(s)
Output :
C:UsersJoshDesktop
C:\Users\Josh\Desktop
Explanation :
Now observe that re.escape function on escaping the special chars in the given string we able to add an other backslash before each backslash, and finally the output results in a double backslash, the desired output.
Hope this helps you.
In python (backslash) is used as an escape character. What this means that in places where you wish to insert a special character (such as newline), you would use the backslash and another character (
n
for newline)
With your example string you would notice that when you put "C:UsersJoshDesktop20130216"
in the repl you will get "C:\Users\Josh\Desktopx8130216"
. This is because 2
has a special meaning in a python string. If you wish to specify then you need to put two
\
in your string.
"C:\Users\Josh\Desktop\28130216"
The other option is to notify python that your entire string must NOT use as an escape character by pre-pending the string with
r
r"C:UsersJoshDesktop20130216"
This is a “raw” string, and very useful in situations where you need to use lots of backslashes such as with regular expression strings.
In case you still wish to replace that single with
\
you would then use:
directory = string.replace(r"C:UsersJoshDesktop20130216", "\", "\\")
Notice that I am not using r'
in the last two strings above. This is because, when you use the r'
form of strings you cannot end that string with a single
Why can't Python's raw string literals end with a single backslash?
You could use
os.path.abspath(path_with_backlash)
it returns the path with