DBWeinstein
DBWeinstein

Reputation: 9479

how do I run command line from python

I have the following command I run in terminal:

mongoexport --db database_name --collection agents --type=csv --fieldFile agFieldsTest.txt --out file/path/agTestInfo.csv

I tried to run it using:

>>> import subprocess
>>> subprocess.call(["mongoexport --db database_name --collection agents --type=csv --fieldFile agFieldsTest.txt --out file/path/agTestInfo.csv"])

I get the following error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 524, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1308, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

Upvotes: 1

Views: 108

Answers (1)

Greg Hewgill
Greg Hewgill

Reputation: 992927

The best way is to break up the command into individual "words":

>>> subprocess.call(["mongoexport", "--db", "database_name", "--collection", "agents", "--type=csv", "--fieldFile", "agFieldsTest.txt", "--out", "file/path/agTestInfo.csv"])

Alternatively, you can use shell=True to have the shell do that for you:

>>> subprocess.call(["mongoexport --db database_name --collection agents --type=csv --fieldFile agFieldsTest.txt --out file/path/agTestInfo.csv"], shell=True)

Upvotes: 2

Related Questions