Python subprocess issue with ampersands

I am currently having a problem with a python script. The script executes arbitrary commands through a handler to convert the invalid error message to the correct error message.

The problem I ran into is getting the script to work correctly with windows using a command containing ampersands in this path. I tried to quote the command avoiding the ampersand with ^, and none of them work. I have no ideas now. Any suggestions?

To clarify current answers:

  • I use the subprocess module
  • I pass the command line + arguments as a list
  • The problem is with the path of the command itself, not with the arguments
  • I tried to quote the command. It causes an error[Error 123] The filename, directory name, or volume label syntax is incorrect
  • I do not use shell argument (s shell=false)
  • In case that matters, I grab the tube for stderr to handle it, but ignore stdout and stdin
  • It is used only for Windows and works as expected in all other cases that I have tested so far.
  • Team Failure:

p = subprocess.Popen (prog, stderr = subprocess.PIPE, bufsize = -1)

when the first element of the prog list contains any ampersands. Quoting this first line does not work.

+3
source share
6 answers

Make sure you use lists and do not extend the shell:

subprocess.Popen(['command', 'argument1', 'argument2'], shell=False)
+5
source

. ? ? ? shell = False ( ) ?

+1

, &

wget "http://foo.com/?bar=baz&baz=bar"

, Linux

+1

", <" >

, ^ escape- Windows? \?

+1

:

( - ), , , .

- .

0

:

exe = 'C:/Program Files (x86)/VideoLAN/VLC/VLC.exe'
url = 'http://translate.google.com/translate_tts?tl=en&q=hello+world'
subprocess.Popen([exe, url.replace("&","^&")],shell=True)

.

0

Source: https://habr.com/ru/post/1697377/


All Articles