1

if I use replace function then also I get an error.

filename = input("copy the file path with it's name and extension and paste it here to Encrypt: ")

filename_replace = filename.replace('\ ', " ")

ERROR says:

Anomalous backslash in string: '\ '. String constant might be missing an r prefix.
vandit vasa
  • 413
  • 4
  • 20

3 Answers3

4

You need to escape the backslash as \\.

filename = input("copy the file path with it's name and extension and paste it here to Encrypt: ")
# say it's something like "c:\myfiles\test.txt"
filename_replace = filename.replace("\\"," ")
# becomes "c: myfiles test.txt"

You can read more about escape characters and string literals here: https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals

Joe DF
  • 5,438
  • 6
  • 41
  • 63
1

Try filename.replace('\\', " ").

Nam V. Do
  • 630
  • 6
  • 27
0

In a python string, backslash is an escape character. This means it is not treated as a backslash, it has special functions (i.e. '\n' is a newline, and 'what\'s your name' == "what's your name"). You can either use two backslashes (to get one, four for two, etc) or put an r before the string (this ignores all backslashes)

r'\ ' == '\\ '

Scrapper142
  • 566
  • 1
  • 3
  • 12