bash

Run Shell Commands from Python on Your Android Device

Want to back up your photo folder every night without touching an app? Or a script that pings you when storage runs low? Both are possible once you know how to call shell commands from Python.

Python lets you hand off tasks to the system shell, whether you're running on a desktop, a server, or an Android device with shell access. This guide walks through the two main approaches and when to use each.

Running Commands the Simple Way: os.system()

The quickest route is os.system(). Import the os module, pass your command as a string, and Python executes it directly in the shell.

The function prints any command output to your screen and returns an exit code. Zero means success; anything else signals an error. You can store this value to decide what happens next—retry, log the failure, or halt the script.

This works fine for simple, one-off tasks. But for anything more involved, you'll hit its limits quickly.

Subprocess: The Method You Should Actually Use

The subprocess module is what Python's own documentation recommends. It gives you fine-grained control over how commands run.

Instead of a single string, subprocess.run() expects a list. The first item is the command name; everything after represents its flags and arguments. Where you'd type ls -lh in a terminal, you'd pass ["ls", "-lh"] to the function.

The returned object holds the exit status in its returncode attribute. You can also suppress output by redirecting stdout to subprocess.DEVNULL—useful when a command spews text you don't need but still want to run.

To capture output instead, use subprocess.PIPE and read it back as text or bytes. For commands that expect input, stream data through the input parameter.

Why This Matters for Android

These techniques bridge the gap between "I downloaded an app" and "I automated a workflow." Android restricts direct shell access by default, but environments like Termux provide the access needed to run Python scripts that execute system commands.

The real advantage: skills transfer across platforms. Scripts you write on a Linux desktop often need only minor tweaks to run on your phone. That portability makes the investment worthwhile.

The trade-off is syntax. The list-based argument style trips up beginners. But once you see that ["echo", "hello world"] breaks down to "echo" followed by its arguments, it clicks.

Quick Reference

Method Input Format Output Control Recommended For
os.system() String Prints to console Quick one-liners, debugging
subprocess.run() List Capture, suppress, or pipe Anything that needs reliability

What You Can Build

A practical example: write a script that lists your largest folders. Import subprocess, run du -sh on your storage directory, capture the output, and sort it. Run that whenever you wonder where all your space went.

Automate it further—have the script write results to a log file you check monthly, or trigger it from a home screen shortcut.

Bottom Line

Python shell commands give you a programmable way to talk to your device. The subprocess module is the right tool for most jobs: flexible, captures what you need, handles errors cleanly.

Start small—one command, check the return code, build up from there.

—

Key takeaways

  • Use os.system() for quick tests and simple one-off tasks
  • Default to subprocess.run() for anything that matters
  • Exit code zero signals success; any other number signals an error
  • Capture output with subprocess.PIPE; suppress it with DEVNULL
  • Learn the list format once, and it works across nearly any command

Frequently asked questions

Can I run Python shell commands on standard Android?

Standard Android doesn't include Python by default. Apps like Pydroid 3 work for running Python code, but full shell access typically requires an environment like Termux.

What's the difference between os.system() and subprocess.run()?

Os.system() is simpler—it runs a command and returns an exit code. Subprocess.run() gives you control over input, output, and error handling, and lets you capture what the command produces.

Do I need Linux knowledge to use subprocess?

Basic familiarity helps, but you don't need to be an expert. Knowing a few common commands like ls, cd, and du gets you far for everyday automation tasks.

Can I chain commands together?

Yes. For complex pipelines, you can run individual commands and pass outputs as inputs to the next step. Simpler cases can use shell=True, though this has security implications to consider.

How do I check if a command failed?

Subprocess.run() returns an object with a returncode attribute. Zero means success; any other number indicates an error. Check this in your code and handle failures accordingly.

Related