[英]WindowsError: [Error 2] The system cannot find the file specified, cannot resolve in Python
[英]Python Error: WindowsError: [Error 2] The system cannot find the file specified:
如下代碼:
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:[Error 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在后台運行。
在Windows中,最佳做法是使用類似路徑。
working_directory = r'C:\Users\Admin\Downloads'
和使用
import os
file_path = os.path.join(working_directory, json_file)
用於路徑串聯。
對於Windows,您有錯誤的斜杠,應該是反斜杠\\\\
而不是正斜杠。
錯誤代碼2表示找不到文件( https://msdn.microsoft.com/zh-cn/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\\\\'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.