Hi Pythonistas!
Sometimes, we need to interact with the operating system while writing Python scripts—like listing files, checking system resources, or even automating tasks. Python’s os module provides a quick and easy way to run shell commands.
Let’s explore how to use os.system() and its pros and cons.
Executing Shell Commands with os.system()
The os.system() function allows us to run a command as if we typed it in a terminal (or Command Prompt on Windows).
>>> import os
>>> os.system('ls -la')
total 43468
drwxr-x--- 22 afsal afsal 4096 Jan 30 19:28 .
drwxr-xr-x 3 root root 4096 Oct 12 11:52 ..
-rw------- 1 afsal afsal 40672 Jan 30 19:28 .bash_history
-rw-r--r-- 1 afsal afsal 220 Oct 12 11:52 .bash_logout
-rw-r--r-- 1 afsal afsal 4499 Nov 16 16:31 .bashrc
drwxrwxr-x 3 afsal afsal 4096 Nov 16 16:34 build_generation
drwx------ 20 afsal afsal 4096 Dec 24 19:09 .cache
drwx------ 20 afsal afsal 4096 Jan 16 11:42 .config
-rw-rw-r-- 1 afsal afsal 582 Jan 16 11:41 csv_to_json.py
drwxr-xr-x 5 afsal afsal 4096 Jan 1 08:07 Desktop
drwxr-xr-x 2 afsal afsal 4096 Jan 7 12:47 Documents
drwxr-xr-x 4 afsal afsal 20480 Jan 28 10:19 Downloads
-rw-rw-r-- 1 afsal afsal 52 Nov 18 10:17 .gitconfig
drwx------ 2 afsal afsal 4096 Dec 10 08:26 .gnupg
-rw-rw-r-- 1 afsal afsal 7651782 Jan 16 11:37 instrument.csv
-rw-rw-r-- 1 afsal afsal 36647434 Jan 16 11:42 instrument.json
drwxrwxr-x 3 afsal afsal 4096 Nov 22 11:12 .ipython
-rw------- 1 afsal afsal 20 Jan 30 14:45 .lesshst
drwx------ 3 afsal afsal 4096 Oct 12 12:38 .local
-rw-rw-r-- 1 afsal afsal 71 Jan 16 11:37 .~lock.instrument.csv#
drwx------ 3 afsal afsal 4096 Dec 5 12:22 .mozilla
drwxr-xr-x 2 afsal afsal 4096 Oct 12 12:38 Music
-rw-r--r-- 1 afsal afsal 310 Oct 12 12:39 .pam_environment
drwxrwxr-x 2 afsal afsal 4096 Jan 16 15:08 pem_files
drwxr-xr-x 3 afsal afsal 4096 Nov 22 16:42 Pictures
drwx------ 3 afsal afsal 4096 Oct 12 12:41 .pki
-rw-r--r-- 1 afsal afsal 807 Oct 12 11:52 .profile
-rw------- 1 afsal afsal 800 Jan 21 11:40 .psql_history
drwxr-xr-x 2 afsal afsal 4096 Oct 12 12:38 Public
-rw------- 1 afsal afsal 871 Jan 3 08:45 .python_history
-rw------- 1 afsal afsal 1116 Dec 7 17:21 .python_history-05202.tmp
-rw------- 1 afsal afsal 0 Jan 2 23:25 .python_history-19980.tmp
drwx------ 6 afsal afsal 4096 Nov 21 08:25 snap
-rw------- 1 afsal afsal 725 Jan 28 08:54 .sqlite_history
drwx------ 2 afsal afsal 4096 Jan 28 13:15 .ssh
-rw-r--r-- 1 afsal afsal 0 Oct 12 12:40 .sudo_as_admin_successful
drwxr-xr-x 2 afsal afsal 4096 Oct 12 12:38 Templates
drwx------ 6 afsal afsal 4096 Dec 5 12:22 .thunderbird
drwxr-xr-x 2 afsal afsal 4096 Oct 12 12:38 Videos
0
>>>
This command prints the output directly to the terminal but does not return the output to Python.
Checking Command Exit Status
We can check whether the command ran successfully by inspecting the return value.
>>> status = os.system("ls -la")
>>> if status == 0:
... print("command excecuted successfully")
... else:
... print("Something went wrong")
...
command excecuted successfully
>>>
A return value of 0 means success. Any other value indicates an error.
Advantages:
- Very simple to use
- Works well for commands where we don’t need to capture output.
Disadvantages:
- No way to capture command output within Python.
- Limited error handling.
- Security risks if user input is involved.
Security Warning: Never use os.system() with user input without sanitization, as it can lead to command injection attacks.
code
filename = input("Enter filename to delete: ")
os.system(f"rm {filename}") # A user could enter "file.txt; rm -rf /"
A user could enter "file.txt; rm -rf /" this cause deletion of entire files
For more control over output and error handling, we should use subprocess instead—covered in the next post!