android - Passing string in stdin -


i want create avd (android virtual device) through command line in python. that, need pass string n stdin. have tried following

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

but raises following error

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 exit code 1 

what can do?

there's not working example. subprocess.check_output() returns output child process want execute, not handle process. in other words string object (or maybe bytes object) cannot use manipulate child process.

probably happens script, using subprocess.check_output(), execute child process , wait until finished before continuing. since never able communicate it, finish non-zero return value raise subprocess.calledprocesserror


now, using grep example of command waits on standard input execute (since don't have android virtual device creator installed) this:

#!/usr/bin/env python2.7 import subprocess  external_command = ['/bin/grep', 'stackoverflow'] input_to_send = '''almost every body uses facebook can google can find answer on stackoverflow if you're 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 child process:", stdout_from_child child_process.wait() 

it print "output child process: can find answer on stackoverflow", output grep.

in example, have

  1. used class subprocess.popen create handle child process
    • setting arguments stdin , stdout value subprocess.pipe enables communicate later on process.
  2. used .communicate() method send string standard input. in same step, retrieved standard output , standard error output.
  3. printed standard output retrieved in last step (just show working)
  4. waited child process finished

in python 3.5, it's simpler:

#!/usr/bin/env python3.5 import subprocess  external_command = ['/bin/grep', 'stackoverflow'] input_to_send = '''almost every body uses facebook can google can find answer on stackoverflow if you're old programmer '''  completed_process_result = subprocess.run(args=external_command,                                            input=input_to_send,                                           stdout=subprocess.pipe,                                           universal_newlines=true) print("output child process:", completed_process_result.stdout) 

in example, have:

  • used module function subprocess.run() execute command.
    • the input argument string send standard input of child process
    • the return value used later on retreive output of child process

now have adapt code situation.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -