Python错误:WindowsError:[错误2]系统找不到指定的文件:

时间:2018-03-14 01:32:47

标签: python xml mongodb

以下代码:

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())

给我这个错误(WindowsError:[错误2]系统找不到指定的文件):

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
事情尝试了:

os.path.abspath则。 将文件路径更改为相对,绝对,原始字符串,双反冲。 执行此检查时,mongodb正在后台运行。

2 个答案:

答案 0 :(得分:0)

在Windows中,最好使用类似。

之类的路径
working_directory = r'C:\Users\Admin\Downloads'

并使用

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

用于路径连接。

答案 1 :(得分:0)

Windows的斜杠应该是反斜杠\\而不是正斜杠。

错误代码2表示找不到文件(https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx

您还需要确保文件存在(json文件)。如果您转到Windows中的命令行并运行此命令:

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

将working_directory变量更改为如下可能会解决您的问题 working_directory = 'C:\\Users\\Admin\\Downloads\\'