Skip to content Skip to sidebar Skip to footer

How To Format A Subprocess.call() Statement?

I'm new to subprocess and I'm trying to use subprocess.call instead of os.system. The command would look like this in os.system: os.system('cluster submit {} gs://{}/{}'.format(clu

Solution 1:

First, take a look at the docs for subprocess.call:

subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)

Run the command described by args. Wait for command to complete, then return the returncode attribute.

The command returns the process return code, which is an integer, so your attempt to call subprocess.call(...).strip() is going to fail. If you want the output of the command, you need:

subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False)

Run command with arguments and return its output as a byte string.

This would give you:

output = subprocess.call(["cluster", "submit", "{}", 
    "gs://{}/{}".format(cluster_name, bucket, file)]).strip()

But there are some basic Python problems there, because you've got a bare "{}" on which you are not calling .format, and then you've got "gs://{}/{}" which only has two format markers but three parameters. You want something more like:

output = subprocess.call(["cluster", "submit", cluster_name, 
    "gs://{}/{}".format(bucket, file)]).strip()

When you use shell=True, as in your second example, you would pass a string rather than a list. E.g:

output = subprocess.check_output("cluster submit {} gs://{}/{}".format(
    cluster_name, bucket_name, script), shell=True).strip()

Solution 2:

subprocess.call([
  "cluster",
  "submit",
  str(cluster_name),
  "gs://{}/{}".format(bucket_name, script)
])

Post a Comment for "How To Format A Subprocess.call() Statement?"