1
import subprocess
import sys

proc = subprocess.Popen(["program.exe"], stdin=subprocess.PIPE) #the cmd program opens
proc.communicate(input="filename.txt") #here the filename should be entered (runs)
#then the program asks to enter a number:
proc.communicate(input="1") #(the cmd stops here and nothing is passed)
proc.communicate(input="2") # (same not passing anything)

How do I pass and communicate with the cmd using python.

(using windows platform)

2
  • Why don't you also paste the relevant parts of the receiver process here? Commented Oct 15, 2012 at 11:29
  • can try something like proc.stdin.write(data_to_write) Commented Oct 15, 2012 at 11:31

1 Answer 1

6

The docs on communicate() explain this:

Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate.

communicate() blocks once the input has been sent until the program finishes executing. In your example, the program waits for more input after you send "1", but Python waits for it to exit before it gets to the next line, meaning the whole thing deadlocks.

If you want to read and write a lot interchangeably, make pipes to stdin/stdout and write/read to/from them.

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

2 Comments

Thanks it helps me understand now what to do next.
@ user1717522 -- if you find a satisfying answer to your question, you should mark it as accepted.

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.