Cristian Perez Cristian Perez - 1 year ago 97
Python Question

Error while executing with subprocess

I'm trying to execute a shell command via Python code, but I'm not capable to understand why it is failing.

When printing the command and pasting it to the shell to try executing it directly works perfectly fine, that's the strange part.

From Python I'm getting the following:

/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `/DATA/NGS/ngs_software/bioinfoSoftware/bwa_current/bwa mem ... --threads 4 -T /tmp/samTemp -'

Is there anything I'm missing? My code looks like this, where 'cmd' is the string with the command. The OS is a CentOS with a bash shell:

process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
out = process.stdout.readline()
out = out.decode("utf-8").strip('\n')

Answer Source

Your command contains a process substitution, but Popen runs its command using /bin/sh. When run as /bin/sh, though, bash does not allow process substitutions. You can explicitly request that the command be run with bash using the executable option.

process = subprocess.Popen(cmd, shell=True, executable='/bin/bash', stdout=subprocess.PIPE)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download