Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a multi-line string as an argument to a script in Windows

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.

like image 301
Zack The Human Avatar asked Apr 14 '09 19:04

Zack The Human


2 Answers

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.

like image 166
Dan Menes Avatar answered Sep 27 '22 22:09

Dan Menes


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
like image 38
moinudin Avatar answered Sep 27 '22 22:09

moinudin