Sorry, I am really not a python person, but the situation hit me, and this will be something very obvious for you python people.
I am trying to communicate interactively with a spawned console process (in our use case it is a console program that communicates with an older, but still nice HP instrument for measuring some physics variables in an experiment).
I have found some insightful example here:
Running interactive program from within python
But when trying to get inspired (I do not want that timer), I started to write this from scratch, testing with an ftp.exe program that is typically present on a windows box, with the following:
#!/usr/bin/python
import subprocess, sys
mystdout=sys.stdout
p = subprocess.Popen("ftp.exe", stdin=subprocess.PIPE, stdout=subprocess.PIPE) #, shell=True)
sys.stdout=p.stdin
print('bye\n'.encode())
sys.stdout=mystdout
print('done')
But the print('bye\n'.encode())
results in:
print('bye\n'.encode())
TypeError: a bytes-like object is required, not 'str'
I can't use the communicate()
method of subprocess as it seems to be not very interactive (one-time only).
Would you, please, give me a hint where is my dumb? This machine runs Python 3.6.1 on Windows, but a friendlier Linux machine with 3.7.3 gives the same greeting.