4 Replies - 722 Views - Last Post: 15 December 2012 - 05:06 AM Rate Topic: -----

#1 flebber  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 05-December 12

Python Running external programs

Posted 12 December 2012 - 09:32 PM

I have been searching for python docs relating to using python to control external programs read their input and pass them onto other programs. I may be using the wrong search terms due to my lack of knowledge but I keep retrieving information on reading and writing files.

The best result so far was for a python module called pexpect http://www.noah.org/wiki/Pexpect . It has a module script.py which can
script.py 
This implements a command similar to the classic BSD "script" command. This will start a subshell and log all input and output to a file. This demonstrates the interact() method of Pexpect. 



Other than that I found some code examples like this.
import subprocess, os

PIPE = subprocess.PIPE
p = subprocess.Popen("example.exe", stdin=PIPE)

p.stdin.write("10")
#p.stdin.flush()
p.stdin.write("\r") #This might be unecessary
#p.stdin.flush()
p.stdin.write("\n")
#p.stdin.flush()

print "End of Execution"
os.system("PAUSE")


So from that i determine that subprocess is the best docs to review?
http://docs.python.o...subprocess.html

Is This A Good Question/Topic? 1
  • +

Replies To: Python Running external programs

#2 alexr1090  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 124
  • Joined: 08-May 11

Re: Python Running external programs

Posted 13 December 2012 - 06:00 AM

I was taught using os was better than using subprocess. Just what I was taught so anyway I've always used os when trying to run other programs. But inputting and shit... that's beyond me right now. I'd probably use the tool known as google for help. I know it looks like you've tried it. But anyways heres how to start a program with os...
import os
os.startfile(filepath)


Was This Post Helpful? 0
  • +
  • -

#3 flebber  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 05-December 12

Re: Python Running external programs

Posted 13 December 2012 - 12:31 PM

View Postalexr1090, on 13 December 2012 - 06:00 AM, said:

I was taught using os was better than using subprocess. Just what I was taught so anyway I've always used os when trying to run other programs. But inputting and shit... that's beyond me right now. I'd probably use the tool known as google for help. I know it looks like you've tried it. But anyways heres how to start a program with os...
import os
os.startfile(filepath)


Ultimately I want to figure out piping between programs so that he output of 1 feeds another.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

Re: Python Running external programs

Posted 13 December 2012 - 12:46 PM

View Postflebber, on 13 December 2012 - 05:32 AM, said:

So from that i determine that subprocess is the best docs to review?
http://docs.python.o...subprocess.html


Yes, it is.

View Postalexr1090, on 13 December 2012 - 02:00 PM, said:

I was taught using os was better than using subprocess


Then you were taught wrong. Many of os's application-invoking functions have been officially deprecated in favor of the subprocess module and the documentation of system (the one that has not been deprecated) specifically directs the user to the subprocess module if he wants to do things that go beyond what system does (as would be the case here). So at least as far as the official documentation goes, subprocess is clearly the way to go.

Quote

But anyways heres how to start a program with os...
import os
os.startfile(filepath)



No, that's not how you start a program. That's how you open a file with its associated application and according to the docs it only works on Windows.
Was This Post Helpful? 1
  • +
  • -

#5 alexr1090  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 124
  • Joined: 08-May 11

Re: Python Running external programs

Posted 15 December 2012 - 05:05 AM

ya you're right. I should've done a little more research before posting.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1