如何使用python os.system()将命令传递给mongoimport?upsertFields有空格
我正在尝试将文件导入mongoDb Python 3.7、mongoimport、windows。 它可以完美地处理没有空间的字段如何使用python os.system()将命令传递给mongoimport?upsertFields有空格,python,windows,command-line,mongoimport,Python,Windows,Command Line,Mongoimport,我正在尝试将文件导入mongoDb Python 3.7、mongoimport、windows。 它可以完美地处理没有空间的字段 command = '"D:\\Program Files\\bin\\mongoimport.exe" -c _ --mode=merge --upsertFields="Current url",Title -d _ --file="D:\\folder\\folder\\temp.json" --jsonArray' os.system(command)
command = '"D:\\Program Files\\bin\\mongoimport.exe" -c _ --mode=merge --upsertFields="Current url",Title -d _ --file="D:\\folder\\folder\\temp.json" --jsonArray'
os.system(command)
结果:
“D:\Program”未被识别为内部或外部命令
我认为这可能与逃跑有关,但我不知道确切的原因
我试过了
c2 = ["D:\\Program Files\\bin\\mongoimport.exe", '-c _ --mode=merge --upsertFields="Current url",Title -d _ --file="D:\\audiotorah\\audiotorah\\temp.json" --jsonArray']
import subprocess
subprocess.Popen(c2,stdin=subprocess.PIPE,stdout=subprocess.PIPE, bufsize =0)
但它永远不会停止。为了避免转义,请在字符串之前附加一个
r
,使其成为原始字符串
使用操作系统:
import os
command = r'"D:\Program Files\bin\mongoimport.exe" -c _ --mode=merge --upsertFields="Current url",Title -d _ --file="D:\folder\folder\temp.json" --jsonArray'
os.system(command)
使用subprocess.Popen
:
import subprocess, shlex
command = r'"D:\Program Files\bin\mongoimport.exe" -c _ --mode=merge --upsertFields="Current url",Title -d _ --file="D:\audiotorah\audiotorah\temp.json" --jsonArray'
subprocess.Popen(shlex.split(command), stdin=subprocess.PIPE, stdout=subprocess.PIPE)
它不起作用。”D:\Program'未被识别为内部或外部命令、可操作程序或批处理文件。-操作系统