Issue
I need to call a subprocess from an asyncio Python program running on Windows as such:
process.exe -SomeNormalArg -SomeArgField="Some Value with Spaces"
I'm currently running it like so:
config.json:
{
"args": [
"-SomeNormalArg",
"-SomeArgField=\"Some Value with Spaces\""
]
}
program.py:
#!/usr/bin/env python3
import asyncio
import json
args = loads(open('config.json').read())['args']
async def main():
await asyncio.create_subprocess_exec('process.exe', *args)
if __name__ == '__main__':
asyncio.run(main())
...But the process is spawning as process.exe -SomeNormalArg "-SomeArgField=\"Some Value with Spaces\""
.
I've read the Converting an argument sequence to a string on Windows bit from the Python docs, but I can't come up with a way to make this work.
I should mention that create_subprocess_shell()
with a full string of the command functions as a workaround, but it's a messy solution.
Solution
You are overquoting. The quotes in -SomeArgField="Some Value with Spaces"
are only needed to prevent the shell from splitting the argument by whitespace, passing its content as separate arguments to the subprocess. Since you're using create_subrocess_exec
which doesn't go through a shell, you don't have that problem to begin with and don't need to quote at all:
{
"args": [
"-SomeNormalArg",
"-SomeArgField=Some Value with Spaces"
]
}
That appears counter-intuitive if you're used to starting the program from a shell, but otherwise it exactly matches what you actually want to pass to the subprocess. (The subprocess won't parse quotes, the shell or the C runtime does that.)
Answered By - user4815162342
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.