【问题标题】:Using subprocess to run an executable with multiple parameters使用子进程运行具有多个参数的可执行文件
【发布时间】:2020-12-18 19:51:36
【问题描述】:

命令行如下所示:

cd C:\Program Files\Microsoft SQL Server\150\COM

snapshot.exe -Publisher [publisher] -PublisherDB [TEST] -Distributor [dist] -Publication [merge] -ReplicationType 2 -DistributorSecurityMode 1

所以一共有两个命令

到目前为止,我有一些运气:

subprocess.run(["C:\\Program Files\\Microsoft SQL Server\\150\\COM\\snapshot.exe","-Publisher [publisher] -PublisherDB [TEST] -Distributor [dist] -Publication [merge] -ReplicationType 2 -DistributorSecurityMode 1"])

这会运行 snapshot.exe,但显示 -Publisher [publisher] -PublisherDB [TEST] -Distributor [dist] -Publication [merge] -ReplicationType 2 -DistributorSecurityMode 1"] 不是有效参数。

【问题讨论】:

  • 我希望 snapshot.exe 的每个参数都需要是数组中的一个单独条目,即subprocess.run(["C:\\Program Files\\Microsoft SQL Server\\150\\COM\\snapshot.exe","-Publisher","[publisher]", "-PublisherDB", "[TEST]", "-Distributor", "[dist]", "-Publication", "[merge]", "-ReplicationType", "2", "-DistributorSecurityMode", "1"])

标签: python windows subprocess


【解决方案1】:

在对run 的调用中,每个单独的字符串也必须是单独的字符串。

cd 可能不是必需的(大多数明智的工具并不关心它们在哪个目录中运行),但我也会添加一个 cwd 参数,以展示如何在一次调用中完成所有操作。

subprocess.run(
        ["C:\\Program Files\\Microsoft SQL Server\\150\\COM\\snapshot.exe"
        "-Publisher", "[publisher]", "-PublisherDB", "[TEST]",
        "-Distributor", "[dist]", "-Publication", "[merge]",
        "-ReplicationType", "2", "-DistributorSecurityMode", "1"],
    # probably drop this
    cwd="C:\\Program Files\\Microsoft SQL Server\\150\\COM",
    # probably add this
    check=True)

【讨论】:

    猜你喜欢
    • 2016-01-25
    • 2016-09-27
    • 2013-06-19
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 2020-05-19
    相关资源
    最近更新 更多