Joe wrote:
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.



I don't want you getting more confused rather than less - newcomers are sometimes confused by Python's encoding of backslashes and such when printing out strings. What is your evidence for the assertion that c is set to \\n rather than \n?

In Unix, it's easier to avoid this, since the shell lets you enter multi-line strings (note that these lines may wrap in the mail):

[EMAIL PROTECTED] sholden]$ python -c "import sys; print repr(sys.argv[1])" "\n"
'\\n'
[EMAIL PROTECTED] sholden]$ python -c "import sys; print repr(sys.argv[1])" "
> "
'\n'


The first case simply demonstrates that the shell doesn't interpret backslash escape sequences. I used repr() because that's what the interpreter will print if you enter an expression at the interactive prompt. The first case shows that the argument is two characters - you can verify this using the len() function.

The second case shows how (with a sensible command shell) you can provide a newline as an argument.

In Windows we can emulate the first case exactly:
C:\Steve>C:\python24\python -c "import sys; print repr(sys.argv[1])" "\n"
'\\n'

Unfortunately Windows XP's command interpreter doesn't bother to wait until you close a double-quote left open at the end of a line:

C:\Steve>\python24\python -c "import sys; print repr(sys.argv[1])" "
''

So someone else will have to tell you how to do that, but you should be clear about what's happening before you try and correct the problem.

regards
 Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005                      http://www.pycon.org/
Steve Holden                           http://www.holdenweb.com/
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to