Reputation: 9986
I have a logger. I am calling an external process. I capture stdout and stderr seperatly from that process so that I can log them.
I have this error:
Traceback (most recent call last): File "importData.py", line 198, in importData(ftpServerName,ftpU,ftpP,directory,filematch,source,destination) File "importData.py", line 99, in importData p = subprocess.Popen(['mongoimport --db AutoPrivilege -c cars stockvo.json --jsonArray --upsert --drop'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) File "/usr/lib/python2.7/subprocess.py", line 710, in init errread, errwrite) File "/usr/lib/python2.7/subprocess.py", line 1335, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory
my script:
...
#import json file to MongoDB
logger.info(' Import json file to MongoDB')
#subprocess.call('mongoimport --db AutoPrivilege -c cars stockvo.json --jsonArray --upsert --drop',shell=True)
p = subprocess.Popen(["mongoimport --db AutoPrivilege -c cars stockvo.json --jsonArray --upsert --drop"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
if stdout:
logger.info(stdout)
if stderr:
logger.error(stderr)
...
Is there a way to resolved that?
Upvotes: 2
Views: 580
Reputation: 8972
subprocess.Popen
constructor accepts list of args, not string:
p = subprocess.Popen(['mongoimport', '--db', 'AutoPrivilege', '-c',
'cars', 'stockvo.json', '--jsonArray', '--upsert',
'--drop'])...
Upvotes: 3