简体   繁体   中英

double quotes and whitespace as argument to python script in PowerShell

I'm trying to run a python script using PowerShell. In that python script I try to use a command line argument which includes double quotes and whitespace, which does not work properly. I suspect it to be an issue with PowerShell.

The (simplified) python script looks like this:

import sys
print(sys.argv[1])

The following calls and outputs show the problem

python script.py --arg=foo
--arg=foo

python script.py --arg='\"foo\"'
--arg="foo"

python script.py --arg='\"fo o\"'
--arg="fo

But what I need in the end is

--arg="fo o"

It strips away everything after the whitespace. I tested the same script in a Linux bash where it worked (with double quotes around the foo). This seems not to be a python issue but a PowerShell problem, can anyone help me? In the end I want to have a full JSON-String as the argument.

9 Month no answer. Current workaround is to escape " with \" .

Here is the bug: https://github.com/PowerShell/PowerShell/issues/1995

Escaping " with '\`' (backslash + backtick) also works (I cannot include the backtick here in the code-style markdown because I don't know how to escape it... ironic).

Please note that the whole argument with the escaped double quotes has to be encapsulated in the double quotes as well. Putting single quotes around it will not work.

Source: http://www.rlmueller.net/PowerShellEscape.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM