views:

1562

answers:

6

I have a simple python script like so:

import sys

lines = sys.argv[1]

for line in lines.splitlines():
    print line

I want to call it from the command line (or a .bat file) but the first argument may (and probably will) be a string with multiple lines in it. How does one do this?

Of course, this works:

import sys

lines = """This is a string
It has multiple lines
there are three total"""

for line in lines.splitlines():
    print line

But I need to be able to process an argument line-by-line.

EDIT: This is probably more of a Windows command-line problem than a Python problem.

EDIT 2: Thanks for all of the good suggestions. It doesn't look like it's possible. I can't use another shell because I'm actually trying to invoke the script from another program which seems to use the Windows command-line behind the scenes.

+1  A: 

Just enclose the argument in quotes:

$ python args.py "This is a string
> It has multiple lines
> there are three total"
This is a string
It has multiple lines
there are three total
marcog
Can this be done from the Windows command line?
Zack Mulgrew
Apparently not..
skrebbel
A: 

Not sure about the Windows command-line, but would the following work?

> python myscript.py "This is a string\nIt has multiple lines\there are three total"

..or..

> python myscript.py "This is a string\
It has [...]\
there are [...]"

If not, I would suggest installing Cygwin and using a sane shell!

dbr
Neither. First of all, cmd does not use \ as escape character, instead it uses ^. Secondly, ^n would just yield a literal n, since ^ can only be used to escape otherwise special characters but it has no capabilities of generating special chars, like \n, \r, etc.
Joey
+1  A: 

The following might work:

C:\> python something.py "This is a string^
More?
More? It has multiple lines^
More?
More? There are three total"
Joey
This lets me effectively span my string across multiple lines but still ends up without the "newline" characters in the string.
Zack Mulgrew
The empty lines are important in this case. At least it worked for echo to output multiple lines but I couldn't test here otherwise at the moment. If all else fails, use the pipeline solution others suggested. Passing multiple lines through stdin is no problem, at least.
Joey
A: 

Have you tried setting you multiline text as a variable and then passing the expansion of that into your script. For example:

set Text="This is a string
It has multiple lines
there are three total"
python args.py %Text%

Alternatively, instead of reading an argument you could read from standard in.

import sys

for line in iter(sys.stdin.readline, ''):
    print line

On Linux you would pipe the multiline text to the standard input of args.py.

$ <command-that-produces-text> | python args.py

mattkemp
Does not seem to work at all. Not directly from the windows command line, and not from a batch file.
skrebbel
+1  A: 

This is the only thing which worked for me:

C:\> python a.py This" "is" "a" "string^
More?
More? It" "has" "multiple" "lines^
More?
More? There" "are" "three" "total

For me Johannes' solution invokes the python interpreter at the end of the first line, so I don't have the chance to pass additional lines.

But you said you are calling the python script from another process, not from the command line. Then why don't you use dbr' solution? This worked for me as a Ruby script:

puts `python a.py "This is a string\nIt has multiple lines\nThere are three total"`

And in what language are you writing the program which calls the python script? The issue you have is with argument passing, not with the windows shell, not with Python...

Finally, as mattkemp said, I also suggest you use the standard input to read your multi-line argument, avoiding command line magic.

Romulo A. Ceccon
+1  A: 

I know this thread is pretty old, but I came across it while trying to solve a similar problem, and others might as well, so let me show you how I solved it.

This works at least on Windows XP Pro, with Zack's code in a file called
"C:\Scratch\test.py":

C:\Scratch>test.py "This is a string"^
More?
More? "It has multiple lines"^
More?
More? "There are three total"
This is a string
It has multiple lines
There are three total

C:\Scratch>

This is a little more readable than Romulo's solution above.

Dan Menes