-2

How do I make a python program text pass as input to another process? Specifically a command shell, not command line!

Not run as

example.exe --doSomething -i random.txt -o random1.txt

but as

example.exe
# example shell loading
# and then in loaded shell
> doSomething -i random.txt -o random1.txt

EDITED POST:

How do I make a python program pass input to another window at the command line? I want to do this:

something = raw_input('Waddaya wanna do?')
if something == 'Halt!':
        placeholder('exit')
if something == 'Get help!':
        placeholder('help %COMMAND%')

placeholder() stands for the command which will pass what is in the brackets to the command shell. I. E. if processname = java.exe, it would pass 'exit' to 'java.exe.'

vorboyvo
  • 9
  • 2

2 Answers2

0

It seems you're seeking for subprocess.popen, see an example in a tutorial:

Writing to a process can be done in a very similar way. If we want to send data to the process's stdin, we need to create the Popen object with stdin=subprocess.PIPE. To test it let's write another program (write_to_stdin.py) which simply prints Received: and then repeats the message we send it:

# write_to_stdin.py
import sys
input = sys.stdin.read()
sys.stdout.write('Received: %s'%input)

To send a message to stdin, we pass the string we want to send as the input argument to communicate():

>>> proc = subprocess.Popen(['python', 'write_to_stdin.py'],  stdin=subprocess.PIPE)
>>> proc.communicate('Hello?')
Received: Hello?(None, None)
alko
  • 46,136
  • 12
  • 94
  • 102
0

The basic gist is you want to use subprocess and pass in the stdin argument and the stdout and stderr arguments as PIPE.

 p = subprocess.Popen(args, *,
                      stdout=subprocess.PIPE,
                      stdin=subprocess.PIPE)

This allows you to use p to send and receive messages to the subprocess:

p.stdin.write('Some input\n')
...
x = p.stdout.readline()
...

Here are some good examples:

read subprocess stdout line by line

Python - How do I pass a string into subprocess.Popen (using the stdin argument)?

Community
  • 1
  • 1
Paul Rubel
  • 26,632
  • 7
  • 60
  • 80