如何使用 python os.system() 将命令传递给 mongoimport? upsertFields 有空格

How do I pass command to mongoimport using python os.system()? upsertFields has spaces

我正在尝试将文件导入 mongoDb。

Python3.7,mongoimport,windows。 它与没有 space.

的字段完美配合
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 使其成为原始字符串。

使用os.system

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)