Advertisement
DigitalMag

Popen stdin/stdout

Feb 2nd, 2021 (edited)
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.84 KB | None | 0 0
  1.         proc = subprocess.Popen(
  2.             cmd_line,
  3.             shell=True,
  4.             stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT
  5.         )
  6.  
  7.         # взаимодействие
  8.         while True:
  9.             proc.stdin.write(b'Y')
  10.             proc.stdin.flush()
  11.             result = proc.stdout.readline()
  12.             print(result.decode('cp866'))
  13.             proc.stdin.write(b'Y')
  14.  
  15.         # одноразовый ввод, чтение:
  16.         proc.stdin.write(b'y' + b'\n')
  17.         proc.stdin.flush()
  18.         proc.stdin.close()
  19.         result = proc.stdout.read()
  20.  
  21.         # question, _ = proc.communicate(b'y')
  22.         # print(question.decode('cp866'))
  23.  
  24. # см так же тут https://stackoverflow.com/questions/30133357/python-subprocess-popen-stdin-write более низкоуровневое
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement