使用Python脚本打开终端并在该新打开的终端中执行终端命令
问题描述:
我需要创建一个Python脚本以在Mac/Linux中打开一个终端窗口,并需要从Python脚本在终端中执行以下命令.
I need to create a Python script to open a terminal window in Mac/Linux and need to execute the following commands in terminal from Python script.
- 打开终端
- 执行命令sudo openpyn --init"
- 它将提示我们输入密码和用户名-从Python脚本传递它
请尝试以下内容,但不起作用,请提出任何想法或建议.将对您有很大帮助.
Tried with below but it is not working,Any idea or recommendations please.Will be of great help.
from openpyn import openpyn
import subprocess
import os
subprocess.call(["sudo openpyn --init"], shell=True)
os.system("sudo openpyn --init")
但是它没有给出任何结果或无法正常工作.
But it is not giving any results or not working as expected.
答
我不完全理解您的问题的措辞,但是如果您要弹出终端并运行:
I don't fully understand the wording of your question, but if you want to pop up a Terminal and run:
ls -l
在该终端中,您可以执行以下操作:
in that Terminal, you can do:
import os
os.system("""osascript -e 'tell application "Terminal" to do script "ls -l"'""")