![](/img/trans.png)
[英]How to run a cmd command in Python using subprocess.run()
[英]Sending CMD command prompts using Subprocess (Python)
对于subprocess
任何专业人员,这都是初学者级的问题。
在Windows中,我可以使用subprocess
发送以下CMD命令,以便它们在单个shell中一个接一个地执行 :
实际上,我正在尝试加载Virtualenv,而无需手动自己触摸CMD提示符。
提前致谢 :)
就像在&&
注释中提到的那样:
from subprocess import call
call(r'cd C:\ && echo 123 && dir', shell=True)
请注意shell=True
参数。
根据评论进行编辑:
如果要将原始输入值传递给调用, Shell=True
是一个安全问题。 从docs看到这个例子:
from subprocess import call
filename = input("What file would you like to display?\n")
>>> What file would you like to display?
>>> non_existent; rm -rf / #
call("cat " + filename, shell=True) # Uh-oh. This will end badly...
最初,您想为个人目的制作一个小脚本。 如果要放弃此代码,请考虑通过distutils或setuptools打包代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.