optparse escaping control characters

Dan Halligan dan.halligan at gmail.com
Tue Aug 19 15:02:12 CEST 2008

On Aug 19, 1:45 pm, Hrvoje Niksic <hnik... at xemacs.org> wrote:
> wannymaho... at gmail.com writes:
> > optparse seems to be escaping control characters that I pass as
> > arguments on the command line.  Is this a bug?  Am I missing
> > something?  Can this be prevented, or worked around?
> It has nothing to do with optparse, it's how Python prints strings:
> $ python -c 'import sys; print sys.argv' '\t'
> ['-c', '\\t']
> Note that you're not really passing a control character to Python,
> you're passing a two-character string consisting of \ and t.  When
> representing the string inside a data structure, Python escapes the \
> to avoid confusion with a real control character such as \t.
> If you try printing the string itself, you'll see that everything is
> correct:
> $ python -c 'import sys; print sys.argv[1]' '\t'
> \t

Thanks for the reply, much clearer now, just one more question.  How
would I pass a control character to python on the command line?

More information about the Python-list mailing list