【问题标题】:How do I pass command to mongoimport using python os.system()? upsertFields has spaces如何使用 python os.system() 将命令传递给 mongoimport? upsertFields 有空格
【发布时间】:2020-03-12 02:42:05
【问题描述】:

我正在尝试将文件导入 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)

结果: '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)

但它永远不会停止。

【问题讨论】:

    标签: python windows command-line mongoimport


    【解决方案1】:

    为避免转义,请在字符串前附加 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)
    

    【讨论】:

    • 它不起作用。 'D:\Program' 不是内部或外部命令、可运行程序或批处理文件。 -- os.system
    猜你喜欢
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 2019-07-02
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多