The following code:
import subprocess
collection = filename[:filename.find('.')]
working_directory = 'C://Users//Admin//Downloads//'
json_file = filename + '.json'
mongoimport_cmd = 'mongoimport -h 127.0.0.1:27017 ' + \
'--db ' + db_name + \
' --collection ' + collection + \
' --file ' + working_directory + json_file
# Before importing, drop collection if it exists (i.e. a re-run)
if collection in db.collection_names():
print 'Dropping collection: ' + collection
db[collection].drop()
# Execute the command
print 'Executing: ' + mongoimport_cmd
subprocess.call(mongoimport_cmd.split())
Is giving me this error (WindowsError: [Error 2] The system cannot find the file specified):
Executing: mongoimport -h 127.0.0.1:27017 --db sacramento --collection sacramento --file C:/Users/Admin/Downloads/sacramento.osm.json
---------------------------------------------------------------------------
WindowsError Traceback (most recent call last)
<ipython-input-232-09c1f8f6a3e4> in <module>()
16 print 'Executing: ' + mongoimport_cmd
17
---> 18 subprocess.call(mongoimport_cmd.split())
C:\Users\Admin\Anaconda2\envs\DAND\lib\subprocess.pyc in call(*popenargs, **kwargs)
521 retcode = call(["ls", "-l"])
522 """
--> 523 return Popen(*popenargs, **kwargs).wait()
524
525
C:\Users\Admin\Anaconda2\envs\DAND\lib\subprocess.pyc in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags)
709 p2cread, p2cwrite,
710 c2pread, c2pwrite,
--> 711 errread, errwrite)
712 except Exception:
713 # Preserve original exception in case os.close raises.
C:\Users\Admin\Anaconda2\envs\DAND\lib\subprocess.pyc in _execute_child(self, args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, to_close, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite)
957 env,
958 cwd,
--> 959 startupinfo)
960 except pywintypes.error, e:
961 # Translate pywintypes.error to WindowsError, which is
WindowsError: [Error 2] The system cannot find the file specified
Things tried:
os.path.abspath. changing file path to relative, absolute, raw string, double backlash. mongodb is running in the background when doing this check.