wagner-felix
wagner-felix

Reputation: 875

Python subprocess.check_call issue

I know I'm doing something wrong.

In the shell i would type:

cuebreakpoints cuefile.cue | shnsplit -o flac flacfile.flac

This would then split the flac file according to the cuefile. Since I'm in the process of writing a small help tool (in Python) to convert flac files I obviously wanted to incorporate this bit in my code.

So in a pythonic way I wrote:

for root, dirs, files in os.walk(args):
    ...
    cmd = ('cuebreakpoints', cue, '|', 'shnsplit', '-o', 'flac', flacs[0])
    subprocess.check_call(cmd, cwd=None)
    ....

'cue' being the cuefile and 'flacs[0]' being the flac file. However I get a:

subprocess.CalledProcessError: Command '('cuebreakpoints', '41_30sec.cue', '|', 'shnsplit', '-o', 'flac', '41_30sec.flac')' returned non-zero exit status 1

Is there a problem because of the PIPE ?

Upvotes: 0

Views: 966

Answers (3)

pcalcao
pcalcao

Reputation: 15990

A little sidenote here.

When writing python scripts using subcommand, I find shlex really helpful to parse more complex commands without worrying too much about how the list will look.

import shlex

...
cmd = "cuebreakpoints '%s' | shnsplit -o flac '%s'" % (cue, flacs[0])
args = shlex.split(cmd)

subprocess.call(args ...)

Upvotes: 0

jcollado
jcollado

Reputation: 40394

As an alternative to passing the command string to a shell (and skip the security problems pointed out by larsmans), you can create two subprocess.Popen objects and connect the output of the first one to the input of the second one (that's basically what the shell will do for you):

For an example about how to do that, please have a look at the replacing shell pipeline section in the documentation.

Upvotes: 2

Fred Foo
Fred Foo

Reputation: 363627

If you want to use shell features such as pipes, you need to give check_call a shell=True kwarg and give the command as a single string:

check_call("cuebreakpoints '%s' | shnsplit -o flac '%s'" % (cue, flacs[0]),
           shell=True)

But note that shell=True is a potential security hazard when used with unsanitized cue or flacs.

Upvotes: 2

Related Questions