2012-07-19 1 views
1

에서 표준 입력 입력을받을 수 없습니다서브 프로세스 내가 가진 <code>repeat.py</code> 파일 편집</p> <p>나는 두 과정 사이 <code>subprocess</code> 교환 데이터를 사용하는 다른 공정

이 파일은 http://www.doughellmann.com/PyMOTW/subprocess/

import sys 

sys.stderr.write('repeater.py: starting\n') 
sys.stderr.flush() 

while True: 
    next_line = sys.stdin.readline() 
    if not next_line: 
     break 
    sys.stdout.write(next_line) 
    sys.stdout.flush() 

sys.stderr.write('repeater.py: exiting\n') 
sys.stderr.flush() 
에서 예이다

이 파일을 실행하여 ipython

In [1]: import subprocess 

In [2]:  f=subprocess.Popen(['python','~/repeat.py'],shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE) 

In [3]: f.stdin.write('teststs\n') 

In [4]: f.communicate() 
Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
NameError: name 'teststs' is not defined 
Out[4]: ('', None) 

teststs이 정의되지 않은 이유는 무엇입니까?

+0

@Sven Marnach가 귀하의 의견을 보내 주셔서 감사합니다.'shell = True'를 제거한 후 괜찮습니다. – timger

+0

괜찮 았음 - 답변을 답글로 바꾸 었습니다. –

+0

related : [subprocess 'docs]에서 shell = True와 함께 list 인수를 사용하지 마십시오 (http://bugs.python.org/issue21347) – jfs

답변

3

repeat.py 대신 대화식 Python 세션을 시작한 것 같습니다. shell=True을 제거해보십시오. 매개 변수 목록과 함께 이해가되지 않습니다. (shell=True을 사용하는 것은 거의 항상 나쁜 생각입니다.)

-1

처음 5 번의 키 누름에서 이상하게 작동합니다. 나는 이유를 모른다. 그 후 잘 작동한다면 ls -l, cd에 액세스 할 수 있습니다. 이전 명령은 UP 키를 누를 때 명령 줄에 모든 기능이있는 것 같습니다.

Here과 비슷한 질문입니다.

관련 문제