0

So I decided to create very simple "ADB Installer" Python app for installing builds and taking screenshots on Android devices using the os.system / os.popen lines, like for example:

os.system("adb connect " + IP)

etc. But now I am kind of stuck, because i need to send this (which works OK in bash script I use as a base for my Python app):

  adb shell "
  cd [path] 
  rm -r [app name]
  exit
  "

How do I do this using os.system / os.popen please? (I really tried to avoid using adb-shell and other Python implementations, but if there is no other way then I will try it). Thanks!

2
  • Can you clarify why os.system doesn't work for you? Isn't the string in the adb shell example just a param to the command? Commented Aug 9, 2020 at 13:03
  • Sorry for not being clear, maybe the os.system will work, I am just not sure how to write it (I am really a beginner with Python, sorry). Thanks! Commented Aug 9, 2020 at 13:06

2 Answers 2

1

Using subprocess:

from subprocess import run, PIPE

path = "[path]"
app_name = "[app name]"
commands_array = ["adb","shell", "rm", path + "/" + app_name , "&&", 
                  "ls", "-la", path]
try:
    result = run(commands_array, stdout=PIPE, stderr=PIPE, 
                  check=True, universal_newlines=True)
except Exception as e:
   print("An error occured:")
   print(e)

print(result.stdout)
Sign up to request clarification or add additional context in comments.

Comments

0

Using triplequotes, you can get multiple lines within a string. Not sure if this'll work, but it's what I would try.

os.system("""adb shell "
  cd [path] 
  rm -r [app name]
  exit
  " """)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.