首页 > 解决方案 > 使用子进程运行具有多个参数的可执行文件

问题描述

命令行如下所示:

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"]不是有效参数。

标签: pythonwindowssubprocess

解决方案


在对 的调用中,每个单独的字符串也必须是单独的字符串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)

推荐阅读