5

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:

  1. I am using the subprocess module
  2. I am passing the command line + arguments in as a list
  3. The issue is with the path to the command itself, not any of the arguments
  4. I've tried quoting the command. It causes a [Error 123] The filename, directory name, or volume label syntax is incorrect error
  5. I'm using no shell argument (so shell=false)
  6. In case it matters, I'm grabbing a pipe to stderr for processing it, but ignoring stdout and stdin
  7. It is only for use on Windows currently, and works as expected in all other cases that I've tested so far.
  8. 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.

rypel
  • 4,686
  • 2
  • 25
  • 36
workmad3
  • 25,101
  • 4
  • 35
  • 56
  • The "script" means what, exactly? What works on windows? What fails on other OS? What other OS? – S.Lott Sep 23 '08 at 12:41

5 Answers5

6

Make sure you are using lists and no shell expansion:

subprocess.Popen(['command', 'argument1', 'argument2'], shell=False)
Armin Ronacher
  • 31,998
  • 13
  • 65
  • 69
  • 3
    @workmad3: out of curiosity, which part of this answer helped? You already gave the syntax for `Popen` above! – Katriel Apr 14 '12 at 18:59
0

Try quoting the argument that contains the &

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

Is usually what has to be done in a Linux shell

Mez
  • 24,430
  • 14
  • 71
  • 93
0

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.

workmad3
  • 25,101
  • 4
  • 35
  • 56
  • If you can give us some *actual information* about what you're doing and how it fails, we might be able to give you a solution. Show us what you're doing. Show us code and failure. Here's it working: >>> subprocess.call([r"c:\spam&ham\python.exe", "-c", ""]) 0 >>> – Thomas Wouters Sep 23 '08 at 12:53
  • how much more actual information do you want? I've said what the error is, why it is occuring and I've now updated with the line that fails for all the help it will give. There isn't any more information I can give. – workmad3 Sep 23 '08 at 13:20
  • 1
    Show us an example of the *actual* pathnames involved, the *actual* error you get including traceback, and how you 'solved' it in the shell-invoking alternative you give here. – Thomas Wouters Sep 23 '08 at 13:27
  • I've said the pathnames (anything containing ampersand isn't enough?) The error is now in place, and the solution is to collapse a list down into a string. I can't give actual pathnames due to not being sure what is covered under my companies NDA. – workmad3 Sep 23 '08 at 13:33
  • Then make a minimal non-working example. I have tested what you are doing with ampersands in a path and *it works* (see above for a short example), so something else is the problem. We can't figure out what until we see actual code. – Thomas Wouters Sep 23 '08 at 13:41
0

"escaping the ampersand with ^"

Are you sure ^ is an escape character to Windows? Shouldn't you use \?

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • Yes ^ is a windows escape character. \ is the directory separator. – workmad3 Sep 23 '08 at 13:38
  • (clarification, ^ is an escape character to the windows command interpreter) – workmad3 Sep 23 '08 at 13:40
  • The arguments to suborocess.Popen( ) are Python strings. Are you sure that the "&" didn't get processed somewhere else and turned into something unexpeccted. What is the actual value of the prog variable? – S.Lott Sep 23 '08 at 14:40
0

I try a situation as following:

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)

This does work.

rickyteng
  • 1
  • 1