PeterHsu
PeterHsu

Reputation: 111

Python sys.argv auto-escaped my arguments from command line?

I am trying to build a script needs the power from 'grep' in linux shell.
However, I found Python sys.argv escaped my regex pattern from command line.
For example, I am going to pass '\d' as grep pattern from command line.
But the string returned by sys.argv[1] was escaped as '\\d'

I did the following test under debug mode:

#SyntaxTest.py
import sys
  #other stuff preventing my debugger from stopping after importing

#shell
python3 -m pdb SyntaxTest.py '\d'

> /cygdrive/d/PythonSandBox/SyntaxTest.py(1)<module>()
-> import sys
(Pdb) n
(Pdb) sys.argv[1]
'\\d'
(Pdb) print(sys.argv[1])
\d

I wonder why Python needs to escape my command line argument and I am asking a way to get a plain, un-escaped string in my example.

EDIT

It's impressive that '\\d'=='\d'.
But in my circumstance, I need to pass the string into subprocess like this.

>>> pattern = '\d'
>>> str = r"echo '%s'"%pattern
>>> str
"echo '\\d'"
>>> subprocess.check_output(str,shell=True)
b'\\d\n'

It's obvious that \\d has been passed to shell. However, I need it to be just \d instead of \\d. Is there any way other than substitute manually?

Upvotes: 0

Views: 1491

Answers (2)

A. Andres
A. Andres

Reputation: 489

Python hasn't escaped your shell argument. This is how the backslash character is represented because backslash is the escape character, so in order to be literally used in the string, it has to be escaped.

For example, you must be aware that print '\n' will print a newline character

// Python REPL:
>>> print 'a\nb'
a
b

If you want to print the literal \n string, you must escape the backslash character with another backslash:

// Python REPL:
>>> print 'a\\nb'
a\nb

So a doubled backslash is just the way that a literal backslash is represented in a Python string (and I guess in almost all programming languages, as this is sort of a standard).

For more information see https://en.wikipedia.org/wiki/Escape_character

Upvotes: 2

John Kugelman
John Kugelman

Reputation: 361595

That's just how the Python interpreter prints strings. Your print call shows that the string isn't really escaped.

>>> '\d'
'\\d'
>>> '\\d'
'\\d'
>>> print('\d')
\d
>>> print('\\d')
\d

Notice also that '\d' and '\\d' are two ways of writing the same string.

>>> '\d' == '\\d'
True

Upvotes: 2

Related Questions