使用Python脚本打开终端并在该新打开的终端中执行终端命令 [英] Open a terminal in Python script and execute terminal commands in that newly opened terminal

查看:170
本文介绍了使用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.

  1. 打开终端
  2. 执行命令sudo openpyn --init"
  3. 它将提示我们输入密码和用户名-从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"'""")

这篇关于使用Python脚本打开终端并在该新打开的终端中执行终端命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆