I'm currently having a major issue with a python script. The script runs arbitrary commands through a handler to convert incorrect error reporting into correct error reporting.
The issue I'm having is getting the script to work correctly on windows with a command that contains ampersands in it's path. I've attempted quoting the command, escaping the ampersand with ^ and neither works. I'm now out of ideas. Any suggestions?
To clarify from current responses:
- I am using the subprocess module
- I am passing the command line + arguments in as a list
- The issue is with the path to the command itself, not any of the arguments
- I've tried quoting the command. It causes a
[Error 123] The filename, directory name, or volume label syntax is incorrect
error - I'm using no shell argument (so
shell=false
) - In case it matters, I'm grabbing a pipe to stderr for processing it, but ignoring stdout and stdin
- It is only for use on Windows currently, and works as expected in all other cases that I've tested so far.
- The command that is failing is:
p = subprocess.Popen(prog, stderr = subprocess.PIPE, bufsize=-1)
when the first element of the list 'prog' contains any ampersands. Quoting this first string does not work.
A proper answer will need more information than that. What are you actually doing? How does it fail? Are you using the subprocess module? Are you passing a list of arguments and shell=False (or no shell argument) or are you actually invoking the shell?
"escaping the ampersand with ^"
Are you sure
^
is an escape character to Windows? Shouldn't you use\
?I try a situation as following:
This does work.
To answer my own question:
Quoting the actual command when passing the parameters as a list doesn't work correctly (command is first item of list) so to solve the issue I turned the list into a space separated string and passed that into subprocess instead.
Better solutions still welcomed.
Try quoting the argument that contains the &
Is usually what has to be done in a Linux shell
Make sure you are using lists and no shell expansion: