2016-11-05 4 views
0

python 명령 줄을 통해 AVD (Android Virtual Device)를 만들고 싶습니다. 이를 위해 문자열 n을 stdin에 전달해야합니다. 내가 무엇을 할 수 나는 다음과 같은stdin에 문자열 전달하기

emulator_create = str(subprocess.check_output([android,'create', 'avd', '-n', emulator_name, '-t', target_id, '-b', abi],stdin=PIPE)) 
emulator_create.communicate("n") 

을 시도했지만 그것은 다음과 같은 오류

raise CalledProcessError(retcode, cmd, output=output) 
subprocess.CalledProcessError: Command '['/home/fahim/Android/Sdk/tools/android', 'create', 'avd', '-n', 'samsung_1', '-t', '5', '-b', 'android-tv/x86']' returned non-zero exit status 1 

Process finished with exit code 1 

을 제기?

+1

오류를 catch하고 예외의'output' 속성을 검사해야합니다. –

답변

0

예를 들어 작동하지 않는 것이 있습니다. subprocess.check_output()은 실행하려는 자식 프로세스의 출력을 이 아니며을이 프로세스의 핸들로 반환합니다. 즉, 자식 프로세스를 조작하는 데 사용할 수없는 문자열 객체 (또는 바이트 객체)를 가져옵니다.

아마도 subprocess.check_output()을 사용하는 스크립트는 하위 프로세스를 실행하고 완료 될 때까지 기다렸다가 계속 진행할 것입니다. 당신이 그것으로 통신 할 수 적이 없기 때문에, 그것은에 표준 입력에 대기 명령의 예로서 grep를 사용하여, 지금 subprocess.CalledProcessError


를 제기 할 0이 아닌 리턴 값으로 종료됩니다

#!/usr/bin/env python2.7 
import subprocess 

external_command = ['/bin/grep', 'StackOverflow'] 
input_to_send = '''Almost every body uses Facebook 
You can also say that about Google 
But you can find an answer on StackOverflow 
Even if you're an old programmer 
''' 

child_process = subprocess.Popen(args=external_command, 
         stdin=subprocess.PIPE, 
         stdout=subprocess.PIPE, 
         universal_newlines=True) 
stdout_from_child, stderr_from_child = child_process.communicate(input_to_send) 
print "Output from child process:", stdout_from_child 
child_process.wait() 

그것은 인쇄됩니다 : (나는 안드로이드 가상 장치 작성자가 설치되어 있지 않기 때문에) 당신이 할 수있는 일을 실행 "자식 프로세스의 출력 :을하지만 당신에 유래에 대한 답변을 찾을 수 있습니다"는 것입니다, grep의 출력. 이 예에서

, 내가 가진

자식 프로세스에 subprocess.PIPE 나중에와 통신하기 위해 우리를있게하는 값
  • 설정 인수 stdinstdout을 핸들을 만들 클래스 subprocess.Popen을 사용
    1. 이 과정.
  • .communicate() 메서드를 사용하여 표준 입력에 문자열을 보냈습니다. 같은 단계에서 표준 출력과 표준 오류 출력을 검색했습니다.
  • 가 (너무 실제로 작동하는지 보여주기 위해) 마지막 단계에서 검색 한 표준 출력을 인쇄
  • 이 자식 프로세스가 파이썬 3.5에서
  • 완료되는 것을 기다렸다, 심지어는 간단 :

    이 예에서

    #!/usr/bin/env python3.5 
    import subprocess 
    
    external_command = ['/bin/grep', 'StackOverflow'] 
    input_to_send = '''Almost every body uses Facebook 
    You can also say that about Google 
    But you can find an answer on StackOverflow 
    Even if you're an old programmer 
    ''' 
    
    completed_process_result = subprocess.run(args=external_command, 
                  input=input_to_send, 
                  stdout=subprocess.PIPE, 
                  universal_newlines=True) 
    print("Output from child process:", completed_process_result.stdout) 
    
    , 내가 가진 :

    • 모듈 기능 subprocess.run()를 사용 명령을 실행합니다.
      • input 인수는 우리가 반환 값은

    이제 당신을 자식 프로세스의 출력을 retreive 나중에에 사용되는 자식 프로세스

  • 의 표준 입력에 보낼 문자열입니다 이 코드를 상황에 맞게 수정해야합니다.

  • 관련 문제