python - 使用子进程运行具有多个参数的可执行文件
问题描述
命令行如下所示:
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"]
不是有效参数。
解决方案
在对 的调用中,每个单独的字符串也必须是单独的字符串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)
推荐阅读
- python - 打包 python 代码依赖项以便即时远程执行
- databricks - 无法在 Databricks 社区版上创建集群
- jmeter - 将外部 ID 保存到 Jmeter 中的结果文件
- amazon-athena - Athena - String to Date 覆盖
- cloud - 是否可以同时在不同的云提供商上使用 SaaS、IaaS 和 PaaS 的单一服务?
- node.js - 如何在 ReactJS 中使用无服务器 Azure SignalR(REST API / 上游)?
- c# - How to insert a variable into a long string with multiple quotation marks and escape characters?
- python - 如何使用 python 打印 json 对象的值?
- python - 正则表达式匹配复杂字符串
- nim-lang - 将日志分析脚本从 Python 转换为 Nim