0

I am calling a command line executable from python:

import subprocess
import os

def genParFiles(program):
    Path = "C:/00Working/99CygwinBin/"

    def exe_call(program):
        fullPath = Path + program
        subprocess.call(fullPath)

    exe_call(program)


if __name__=='__main__':
    main()

This works fine. The exe runs in the interpreter window.

Now, the program that I'm calling is waiting for me to press enter to start calculating, which I can do in the interpreter window with no problem.

My question is, how can I automate the 'enter' so I don't have to press it manually?

4
  • You can try this: noah.org/wiki/pexpect Commented Feb 1, 2014 at 0:18
  • check out the docs for subprocess.call - you can provide an argument for stdin and presumably pass input through that? Commented Feb 1, 2014 at 0:19
  • @kaveman Ok... So what would I pass? I tried subprocess.call(fullPath, stdin=" ") but that did not work. Commented Feb 1, 2014 at 0:34
  • 1
    did you read the docs, at that link? stdin...Valid values are PIPE, an existing file descriptor (a positive integer), an existing file object, and None Commented Feb 1, 2014 at 0:45

1 Answer 1

1

It's fairly straightforward with Popen:

p = subprocess.Popen(fullPath, shell=True, stdin=subprocess.PIPE)
stdO, stdE = p.communicate("foo\n")

::edit:: Fixed communicate call as pointed out in comments.

Sign up to request clarification or add additional context in comments.

4 Comments

If you're using Python 3, then you will want to remove shell=True and add universal_newlines=True
@dg99 Thanks for the help. I am using Python 3. The first line works and is calling the program. But I am getting an error with the 2nd line. "If self._communication_started and input: AttributeError: 'str' object has no attribute '_communication_started'" Any guidance you can give would be appreciated!
@dg99 also, that second line should be subprocess.Popen.communicate right?
@dg99 Nevermind, I got it. I made the 2nd line say: "stdO = p.communicate("foo\n") and that worked. Thanks for the tip.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.