-1

What is the way for preventing Python from interpreting \ followed by numbers as something else?

e.g. I get DirectoryNameFromAnotherProgram (say it is equal to 'N:\Some Directory') print DirectoryNameFromAnotherProgram + '1234.txt'

# prints:
# N:\Some DirectoryS4.txt

Since the string with "\" comes as output from another script, I do not have a choice to change it.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Imagine
  • 85
  • 1
  • 10
  • Why this is not a duplicate question: I know you can add "\" to escape a backslash. That works when I have control over the script input. The issue is that the input to my Python script comes from another program, so I cannot escape what I receive. – Imagine Feb 14 '15 at 01:43
  • 1
    How exactly is the string coming from another script? You may not need to change it at all. If you do `s = input("enter something: "); print(s)`, for example, you don't need to worry about escaping. Unless I'm misinterpreting what you're trying to do. – Andrew Magee Feb 14 '15 at 05:21
  • I was getting a directory name and was adding a filename to it. E.g. getting 'N:\Some Directory\' from another program and then adding '1234.txt' inside my script. Since the post I used os.path.join and it fixes the issue. – Imagine Feb 14 '15 at 19:18
  • "Since the string with "\" comes as output from another script, I do not have a choice to change it." Then there is not actually anything to change (unless you copied and pasted the output into your code, rather than *reading it as a string*. The output *actually contains* a backslash, so no escaping is required. Re-closing as a duplicate. Also, I have *absolutely no idea* why this was tagged `regex`. (Also: please do not edit questions to put attempts at an answer into them; this is **not a discussion forum**.) – Karl Knechtel Aug 07 '22 at 02:52

1 Answers1

1

Put a "\" in front of the "\". The meaning of "\" in a sting is: the next character doesn't mean what it normally means. If the next character was not normally special (for example, if it's a digit), it means something special now. If the next character does normally mean something special (for example, a backslash), it's not special now. Either way, the initial "\" has done its thing, and is removed.

Special case: if the next character is not normally special (for example, the "S" in your string), but cannot be made special (the sequence "\S" has no special meaning), then the backslash doesn't do anything and is not removed.

ganbustein
  • 1,593
  • 11
  • 10
  • Thanks. That works when I have control over the script input. The issue is that the input to my Python script comes from another program, so I cannot escape what I receive – Imagine Feb 14 '15 at 01:41
  • ALWAYS sanitize your inputs. Never blindly plug a string you don't have control of into a string you need to control. That having been said, you always have control over your own literals, which is the only place where you need to do the escaping. Please look again at the answers to the question you think this is not a duplicate of. I cannot improve on the answer posted there by David Z; he tells you several ways to safely incorporate strings you get from elsewhere. – ganbustein Feb 14 '15 at 02:50
  • If the input comes from another program, then *nothing special needs to be done*. Escaping **only** applies to **literal strings in your source code**. – Karl Knechtel Aug 07 '22 at 02:53