0

Why process = subprocess.Popen(command, stdout=subprocess.PIPE) is not working for such command?

['docker', 'exec', 'hungry_keller', 'bash', '-c', ' ""ls -l""] docker exec hungry_keller bash -c "ls -l"

and returns: No such file or directory

subprocess.check_output returns the same error.

but in terminal it executes correctly. More simple commands are working fine.

1 Answers1

1

You've been overzealous with your quoting. When you type at the shell prompt bash -c "ls -l", you're using the quotes to tell bash that ls -l is a single token. Those quotes aren't actually seen by bash at all; it simply receives two arguments:

  1. -c
  2. ls -l

When calling subprocess methods with a list, you're already being explicit about your tokenization because you're providing a list of strings. So instead of:

subprocess.check_output(['docker', 'exec', 'hungry_keller', 'bash', '-c', ' ""ls -l""] )

You want:

subprocess.check_output(['docker', 'exec', 'hungry_keller', 'bash', '-c', 'ls -l'] )
larsks
  • 277,717
  • 41
  • 399
  • 399