0

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.

2 Answers 2

0

In windows it is best practice to use path like.

working_directory = r'C:\Users\Admin\Downloads'

and use

import os
file_path = os.path.join(working_directory, json_file)

For path concatenation.

Sign up to request clarification or add additional context in comments.

7 Comments

alternatively you can do: working_directory = 'C:\\Users\\Admin\\Downloads\\'
Yeah bur using raw string looks preaty.
yes definitely use os.path.join for path concatenation.
This isn't the problem. Windows will use forward slashes just fine.
But why downvote. This is not misguiding answer. This will help op to write more readable code.
|
0

You have the wrong slash for Windows should be backslash \\ instead of the forward slash.

Error code 2 means file not found (https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx)

You also need to make sure the file exists (the json file). If you go to the command line in windows and run this command:

C:\Users\Admin\Downloads>dir  C:\Users\Admin\Downloads\<yourfile>.json
 Volume in drive C is Windows8_OS
 Volume Serial Number is CC65-A251

 Directory of C:\Users\Admin\Downloads

06/03/2017  02:05 PM         2,390,114 <yourfile>.json
               1 File(s)      2,390,114 bytes
               0 Dir(s)  723,947,110,400 bytes free

Changing working_directory variable to be as follows might solve your problem working_directory = 'C:\\Users\\Admin\\Downloads\\'

5 Comments

Windows is perfectly happy with forward slashes (even doubled ones like are being used in the question).
@kindall double slashes are for escaping. Windows is not fine with forward slashes. If anything, it is possible that python will convert if you use certain path api (os.path). However, he is calling subprocess subprocess.call(mongoimport_cmd.split()) where his string for file is C:/Users/Admin/Downloads which doesn't work on Windows. The double slashes is for escaping, double backslash equals to one and same for double forward slashes.
Windows is fine using forward slashes.
@kindall try the above dir command with forward slashes and show me how it delivers correct output.
It produces exactly the same output as the version with the backward slashes. (That exact path doesn't exist on my machine, but dir commands of paths do exist work fine with either type of slash.) Of course, you have to quote the paths because otherwise cmd.exe thinks the slashes begin switches, but that's specific to cmd.exe, and would also occur if you had a hyphen in the path. OP is not using cmd.exe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.