I want to invoke a script, piping the contents of a string to its stdin and retrieving its stdout.
I don\'t want to touch the real filesystem so I can\'t create real
In Python 3.4 and newer, you can use the input keyword parameter to send input via STDIN when using subprocess.check_output()
Quoting from the standard library documentation for subprocess.check_output():
The input argument is passed to
Popen.communicate()and thus to the subprocess’s stdin. If used it must be a byte sequence, or a string ifuniversal_newlines=True. When used, the internalPopenobject is automatically created withstdin=PIPE, and the stdin argument may not be used as well.
Example:
>>> subprocess.check_output(["sed", "-e", "s/foo/bar/"],
... input=b"when in the course of fooman events\n")
b'when in the course of barman events\n'
>>>
>>> # To send and receive strings instead of bytes,
>>> # pass in universal_newlines=True
>>> subprocess.check_output(["sed", "-e", "s/foo/bar/"],
... universal_newlines=True,
... input="when in the course of fooman events\n")
'when in the course of barman events\n'