Search code examples
pythonwindowsstringdirectoryslash

backward slash followed by a number in python strings


I've encountered a problem in Python when dealing with backward slashes followed by numbers inside a string. I use windows OS environment.

This becomes especially annoying when you have numbers in the beginning of a name in a directory.

Ex: "P:\70_parseFile\80_FileDir\60_FA_050"

This was a discovery for me that you can create special characters if you do "\1", "\2", "\3"... and so on. As wonderful as this seems, I have to ask on how to go about turning this off, or what other different string function is there that doesn't have this special feature?

Thanks, all!


Solution

  • You have two choices:

    • Backslash those backslashes:

      "P:\\70_parseFile\\80_FileDir\\60_FA_050"
      
    • Use a raw string, in which the backslash loses its "special meaning"

      r"P:\70_parseFile\80_FileDir\60_FA_050"