How To Format A Subprocess.call() Statement?
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?"