Rohan Gala
Rohan Gala

Reputation: 651

How to Replace' \ ' with' / '

a="D:/R_SVN/hostworkspace/middleware/projects/module/com.ofss.fc.module.ac/src/com/ofss/fc/app\ac\service\writeoffrecovery\ext\WriteoffRecoveryApplicationServiceExtExecutor.java"

b=a.replace('\','/')

print b

Error:

 b=a.replace('\','/')

SyntaxError: EOL while scanning string literal

Upvotes: 2

Views: 131

Answers (3)

Usman Maqbool
Usman Maqbool

Reputation: 3371

In strings \ is escape character e.g if there are two \ like \\ then first one is escape character.

in b=a.replace('\','/') '\' is read as escape character. so you can replace it with \\. In this case first \ will be escaped and second one will perform operation on string a.

code:

>>> a="D:/R_SVN/hostworkspace/middleware/projects/module/com.ofss.fc.module.ac/src/com/ofss/fc/app\ac\service\writeoffrecovery\ext\WriteoffRecoveryApplicationServiceExtExecutor.java"
>>> b=a.replace('\\','/')
>>> print b
D:/R_SVN/hostworkspace/middleware/projects/module/com.ofss.fc.module.ac/src/com/ofss/fc/appc/service/writeoffrecovery/ext/WriteoffRecoveryApplicationServiceExtExecutor.java

Upvotes: 1

Harsha Biyani
Harsha Biyani

Reputation: 7268

As "Backslash notation" is used for "Escape character", you have to add \\ instead of \

a.replace('\\','/')

Upvotes: 3

Jens
Jens

Reputation: 69440

You have to escape the backslash, because it is a special character:

 b=a.replace('\\','/')

Upvotes: 2

Related Questions