Reputation: 1746
When I try calling the below code, I run into the following error: "You must specify a format when providing data via STDIN (pipe)."
subprocess.call(["in2csv", "--format", "xls", a_file, ">", output_file], shell=True)
I'm not sure why this is the case because I am telling it what the initial format is. I've looked at the docs, which isn't clear about the distinction between --format and -f.
Update: I've changed it to use argparse to simplify passing the arguments following this recommendation. I'm also using Popen as used here, which is apparently safer than using shell=true flag according to the docs.
parser = argparse.ArgumentParser()
parser.add_argument('in2csv')
parser.add_argument('--format')
parser.add_argument('xls')
parser.add_argument(a_file)
parser.add_argument(">")
parser.add_argument(output_file)
args = parser.parse_args()
print args
subprocess.Popen(args)
Upvotes: 0
Views: 967
Reputation: 1394
Errors like what you've seen are a symptom of the shell getting confused by the string passed in, for instance because of a space in a filename. It is indeed best to avoid using the shell when spawning processes from Python.
Instead of adding ">" and output_file
as arguments, try redirecting the output using the stdout keyword argument, which takes a file that output will be written to.
Assuming:
a_file
is a string with the name of your input file, andoutput_file
is a string with the name of your desired output file, a working call might look like:
with open(output_file, 'wb') as of:
subprocess.check_call(["in2csv", "--format", "xls", a_file],
stdout=of)
It's not necessary to use argparse here; it's meant for handling command lines coming in to your program, rather than going out from it.
Upvotes: 3