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
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)
Related
When using this library https://github.com/andersinno/python-database-sanitizer, I get an error that it cannot find mysqldump even though it is in my PATH.
edwin$ database-sanitizer mysql://root#localhost/test
Traceback (most recent call last):
File "/Users/edwin/code/test/venv/bin/database-sanitizer", line 11, in <module>
sys.exit(main())
File "/Users/edwin/code/test/venv/lib/python3.6/site-packages/database_sanitizer/__main__.py", line 60, in main
config=config,
File "/Users/edwin/code/test/venv/lib/python3.6/site-packages/database_sanitizer/dump/__init__.py", line 46, in run
for line in db_module.sanitize(url=parsed_url, config=config):
File "/Users/edwin/code/test/venv/lib/python3.6/site-packages/database_sanitizer/dump/mysql.py", line 68, in sanitize
stdout=subprocess.PIPE,
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 729, in __init__
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 1364, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'mysqldump': 'mysqldump'
I also tried in the python interpreter and didn't have the error.
>>> import subprocess
>>> subprocess.Popen('mysqldump')
<subprocess.Popen object at 0x1081e2b38>
>>> Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump --help
I am using OSX.
I have to run HIVE queries using python script eg,
For this $ bdscc -e "show databases"
I want to do the same in python, but I am getting error-
>>> import subprocess
>>> subprocess.call(['bdscc', '-e', 'show databases'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/subprocess.py", line 524, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Is there any way to do this in python?
PS- The command $ bdscc -e "show databases" from terminal is running fine.
UPDATE-- bdscc is an alias set by my company for HIVE shell
I would like to run a very simple hive command from within my python script. I am trying to use hive -e, but I am getting an error
def hive():
cmd = "hive -e \"msck repair table dashboard_report\""
print(cmd)
check_call(cmd)
This is the error I am getting
hive -e "msck repair table dashboard_report"
Traceback (most recent call last):
File "/home/yosi/work/source/slg/tiger/src/main/resources/python/tiger.py", line 59, in <module>
hive()
File "/home/yosi/work/source/slg/tiger/src/main/resources/python/tiger.py", line 57, in hive
check_call(cmd)
File "/usr/lib/python2.7/subprocess.py", line 535, in check_call
retcode = call(*popenargs, **kwargs)
File "/usr/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Your check_call function is calling subprocess.Popen. If you want to pass arguments to that function you have to pass them in a list.
Probably:
cmd = ["hive", "-e", "\"msck repair table dashboard_report\""]
check_call(cmd)
Will work. Maybe some refactoring will be needed down the call stack to accept a list instead of a string.
If you are using python2.7 then the below code snippet will work.
import subprocess
command = [""" hive -e "msck repair table dashboard_report" """]
print subprocess.check_output(command,shell=True)
I am trying to make a Python function that will take a bash command (cmd) as an argument, and then
execute that command.
But I am having some issues...
This is my program:
import subprocess
def main():
runCommand("ls")
runCommand("ls -l")
runCommand("cd /")
runCommand("ls -l")
def runCommand(cmd):
subprocess.Popen(cmd)
It works for commands like "ls" or "who" but when it gets longer such as "ls -l" or "cd /" it gives me an error.
Traceback (most recent call last):
File "<string>", line 1, in ?
File "test.py", line 8, in main
runCommand("ls -l")
File "test.py", line 14, in runCommand
subprocess.Popen(cmd)
File "/usr/lib64/python2.4/subprocess.py", line 550, in __init__
errread, errwrite)
File "/usr/lib64/python2.4/subprocess.py", line 996, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
You need to put your command and its option in a list :
subprocess.Popen(['ls','-l'])
Attempting to call a shell script with options from a python script.
The line ./dropbox_uploader.sh -s download /test/pictures pictures/ runs fine via SSH but errors when called from a python script:
import subprocess
subprocess.call(['./dropbox_uploader.sh -s download /test/pictures pictures/'])
Here is the error message:
Traceback (most recent call last):
File "sync.py", line 2, in <module>
subprocess.call(['./dropbox_uploader.sh -s download /test/pictures pictures/'])
File "/usr/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
In case if the first argument of subprocess.call is a list, it must contain the executable and arguments as separate items:
subprocess.call(['./dropbox_uploader.sh', '-s',
'download', '/test/pictures', 'pictures/'])
or, maybe, more convenient:
import shlex
cmd = './dropbox_uploader.sh -s download /test/pictures pictures/'
subprocess.call(shlex.split(cmd))
There is also an option to delegate parsing and execution to the shell:
cmd = './dropbox_uploader.sh -s download /test/pictures pictures/'
subprocess.call(cmd, shell=True)
(But please note the security warning)