デコレートしたいpython.exe
。例えば、それはすることができますちょうどInput:\n
我々は対話モードでstdout
プレフィックスから読んだとき、我々はstdin
とOutput:\n
に書き込むとき:サブプロセスを使用してCLIプログラムをデコレートする。
オリジナルpython.exe
:
$ python
Python 3.6.1 |Anaconda custom (64-bit)| (default, Mar 22 2017, 20:11:04) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print(2)
2
>>> 2 + 2
4
>>>
除外がpython.exe
を飾ら
$ decorated_python
Output:
Python 3.6.1 |Anaconda custom (64-bit)| (default, Mar 22 2017, 20:11:04) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
Input:
print(2)
Output:
2
>>>
Input:
2 + 2
Output:
4
>>>
私はそれがこのようになるはずだと思います:
import subprocess
pid = subprocess.Popen("python".split(), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
pid.stdin.write(input('Input:\n').encode())
print('Output:\n' + pid.stdout.readlines())
しかし、pid.stdout.readlines()
は実行されていません。
私もcommunicate
メソッドを使用しようとしましたが、それは初回のみ動作します。
import subprocess
pid = subprocess.Popen("python".split(), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
print('Output:\n', pid.communicate(input('Input:\n').encode()))
テスト:
Input:
print(1)
Output:
(b'1\r\n', b'')
Input:
pritn(1)
Traceback (most recent call last):
File "C:/Users/adr-0/OneDrive/Projects/Python/AdrianD/temp/tmp.py", line 6, in <module>
print('Output:\n', pid.communicate(input('Input:\n').encode()))
File "C:\Users\adr-0\Anaconda3.6\lib\subprocess.py", line 811, in communicate
raise ValueError("Cannot send input after starting communication")
ValueError: Cannot send input after starting communication
私はpython
でちょうど2
を置く場合、私がするので、たぶん私はちょうど、何かを欠場2
を入手してください。
ピュアPython:communicate
方法で飾ら
>>> 2
2
:あなたがPythonドキュメントを見れば
Input:
2
Output:
(b'', b'')
お返事ありがとうございます。残念ながら、 'pexpect'はWindowsでは動作しません... – ADR