technazi
technazi

Reputation: 958

python - subprocess - Open terminal with a file

I'm trying to make an automated script which should run a set of commands in the same Terminal Window (Mac OS X Yosemite). But I could not reach the stage where I can continue adding the commands.

Automator.py

path = '/Users/aakashshah/documents/python/test.py'

def subprocess_cmd(command):
    process = subprocess.Popen(command,stdout=subprocess.PIPE, shell=True)
    proc_stdout = process.communicate()[0].strip()
    print(proc_stdout)

subprocess_cmd('cd /Users/aakashshah/documents/python/ | open -a Terminal | python test.py')

Result is that the Terminal application opens with just the root name.

The similar command runs in the terminal with desired output in the same terminal window.

Thankyou. Any help would be appreciated.

Upvotes: 2

Views: 4770

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

You can pipe using Popen and use cwd:

from subprocess import Popen,PIPE


def subprocess_cmd(dr,cmd1,cmd2):
    p1 = Popen(cmd1.split(),stdout=PIPE,cwd=dr)
    p2 = Popen(cmd2.split(),stdin=p1.stdout,stdout=PIPE,cwd=dr)
    p1.stdout.close()
    return p2.communicate()[0]

subprocess_cmd('/Users/aakashshah/documents/python/','open -a Terminal','python test.py')

If you just want to run the python file the first cmd1 is unnecessary.

Upvotes: 1

Related Questions