What is the best way to handle a command line argument that includes an escape sequence like \n?

Joe JoeSalmeri at hotmail.com
Wed Mar 2 18:38:22 EST 2005


I'm using Python 2.4 on Windows XP SP2.

I'm trying to receive a command line argument that is a newline (\n)

Here is the command line to use

sample.py "\n"

Here is a sample.py script

import sys

c = sys.argv[1]

# when run c is set to \\n instead of \n.

I created a test batch file

echo %1

to confirm that it was not the cmd.exe command processor causing the issue.

It appears that Python treats the comand line string as a raw string.

Is this a bug?  If not what is the best way to work around the issue?

Obviously I could use a hack

if c == '\\n':
    c = '\n'

But surely there is a better way.

NOTE that I used \n in my sample but I also want to support other escape 
sequences too.






More information about the Python-list mailing list