Passing a multi-line string as an argument to a script in Windows
Solution 1
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.
Solution 2
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
Solution 3
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.
Solution 4
The following might work:
C:\> python something.py "This is a string^
More?
More? It has multiple lines^
More?
More? There are three total"
Shoaib
I'm a front end engineer located in the Bay Area. I'm passionate about web technologies and the art of programming. I also like: Photography NES games (Mega Man) Wright's Pink Popcorn
Updated on July 26, 2022Comments
-
Shoaib almost 2 years
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.
-
Andrew Hare about 15 yearsI don't understand - does what you have now not work?
-
Lucas Jones about 15 yearsYou should split by "\n" and remove the "\r" beforehand for better platform compatibility. Does
bash
put carraige returns in it's arguments? (Not sure). -
Devin Jeanpierre about 15 yearsYou shouldn't use the string module at all. That line should read
lines = multiline.splitlines()
-
Shoaib about 15 yearsAll good suggestions. My total experience with python amounts to about an hour so far.
-
-
Rich about 15 yearsNeither. 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.
-
Shoaib about 15 yearsThis lets me effectively span my string across multiple lines but still ends up without the "newline" characters in the string.
-
Rich about 15 yearsThe 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.
-
skrebbel over 14 yearsDoes not seem to work at all. Not directly from the windows command line, and not from a batch file.