簡體   English   中英

從python腳本運行終端命令(Linux)

[英]Running terminal commands from python script (Linux)

我正在嘗試編寫一個執行以下操作的python腳本:

  1. 打開一個終端,將cd轉到目錄,然后在其中運行命令。

  2. 打開第二個終端,將cd轉到另一個目錄,然后在該目錄中執行一個文件。

僅在完成步驟2之后才能完成步驟1的執行。 第1步和第2步均應通過python(或其他語言)腳本完成。

我嘗試使用subprocess.Popen,subprocess.call,os.sytem,但這似乎不起作用。

有誰知道如何做到這一點?

我到目前為止的代碼:

import subprocess

terminal1 = subprocess.Popen(["gnome-terminal",'cd ~',"torcs -r ~/quickrace.xml"])
terminal2 = subprocess.Popen(['gnome-terminal','cd ~'])

我不知道這是否正是您想要的,但是嘗試一下,將$ variables替換為您想要的,我認為它將運行。

#!/bin/bash


xterm -e "pwd ; cd $PATH ; your command ; echo press RETURN to close this window ; read" # delete the echo and the read to don't stop the process and make it run quickly
xterm -e "pwd ; cd $OTHERPATH ; your command ; echo press RETURN to close this window ; read"

如果沒有,請考慮安裝xterm。 (如果有rpm軟件包,則為rpm -i xterm)

我在python中做到了這一點,但這並不是您想要的,但這是我的開始。

# -*- coding: utf-8 -*-

import os

if __name__ == "__main__":
    os.system('xterm -e "pwd ; cd $PATH ; your command ; echo press RETURN to close this window ; read" &') # delete the echo and the read to don't stop the process and make it run quickly
    input("Press Enter to continue...")
    os.system('xterm -e "pwd ; cd $OTHERPATH ; your command ; echo press RETURN to close this window ; read" &')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM