谁可以解释并显示示例如何在其中运行外部程序并执行命令?
我想这样做: 1)运行终端的新窗口 2)发送命令以在终端中执行
系统:Mac OS
import subprocess
test = subprocess.Popen(["/usr/bin/open", "-W", "-n", "-a", "/Applications/iTerm.app"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
test.stdin.write("ls")
Traceback (most recent call last):
File "/Users/NEWA/PycharmProjects/TEST/TEST.py", line 3, in <module>
test.stdin.write("ls")
TypeError: a bytes-like object is required, not 'str'
Process finished with exit code 1
谢谢。
答案 0 :(得分:0)
我曾经在Flask and Scrapy项目中使用过此功能,您必须使用
subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None, text=None, env=None, universal_newlines=None)
在此处查看https://docs.python.org/3/library/subprocess.html 有关更多信息