3

I'm trying to escape the backslash, but trying to understand the right way of doing it

foo = r'C:\Users\test.doc'

The above works fine

However, when I want to escape the path stored in a variable

For example :

parser.add_argument('-s', '--source', type = str, help = 'Source file path')

Now, how do escape the value in - args.source

Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
user4723845
  • 151
  • 2
  • 3
  • 9
  • I think characters from an external source are taken literally. – TigerhawkT3 Jul 03 '15 at 20:03
  • Yes, but I want to escape it. Or I get a error No such file or directory: 'C:\\Users\\test.doc – user4723845 Jul 03 '15 at 20:14
  • Is that how the traceback looks? – Padraic Cunningham Jul 03 '15 at 20:30
  • @PadraicCunningham Yes – user4723845 Jul 03 '15 at 20:37
  • 1
    how are you running this that you pass a variable? – Padraic Cunningham Jul 03 '15 at 20:43
  • 1
    `args.source` is populated from the command line, so you need to ensure you quote/escape things properly according to the shell you are using. In other words, it's not a Python issue, it's a shell issue. – chepner Jul 03 '15 at 20:52
  • Possible duplicate of [How to escape backslash and single quote or double quote in Python?](https://stackoverflow.com/q/6717435/608639) – jww Nov 28 '18 at 00:08
  • Please read [mre]. If the traceback looks like that, then there is **nothing wrong with the code**; the problem is that the `Users` folder on the `C` drive really doesn't actually contain a file named `test.doc`. The most likely problem is that you forgot to actually include a sub-folder for *the specific user* who has that file. (and possibly also e.g. a `Desktop` sub-folder under that.) Voting to close as this does not point at a reproducible problem. – Karl Knechtel Aug 07 '22 at 03:34

1 Answers1

5

So there are a few escape sequences to be aware of in python and they are mainly these.

enter image description here

So when the string for that file location is parsed by the add_argument method it may not be interpreted as a raw string like you've declared and there will be no way to escape the backslashes outside of the declaration.

What you can do instead is to keep it as a regular string (removing the 'r' prefix from the string) and using the escape character for a backslash in any places there may be conflict with another escape character (in this case \t). This may work as the method may evaluate the string correctly.

Try declaring your string like this.

foo = "C:\Users\\test.doc"

Hopefully this helps fix your issue!

EDIT:

In response to handling the dynamic file location you could maybe do something like the following!

def clean(s):
    s = s.replace("\t", "\\t")
    s = s.replace("\n", "\\n")
    return s

Until you've covered all of your bases with what locations you may need to work with! This solution might be more appropriate for your needs. It's kind of funny I didn't think of doing it this way before. Hopefully this helps!

Tresdon
  • 1,211
  • 8
  • 18