Python | os.system() method

Last Updated : 20 Mar, 2026

The os.system() method is part of Python’s built-in os module. It is used to execute a system command from within a Python program. The command is passed as a string and runs in the operating system’s command shell.

The output of the command is displayed in the standard output and the method returns the exit status of the executed command.

Example: This example executes a simple system command to display the current working directory.

Python
import os
r = os.system("pwd")
print("Exit Status:", r)

Output
/home/guest/sandbox
Exit Status: 0

Explanation: os.system("pwd") runs the pwd command in the system shell. The returned value stored in r represents the exit status of the command.

Syntax

os.system(command)

Parameters: command - A string that specifies the system command to execute.

Return Value: Returns the exit status code of the executed command.

  • 0 usually means success.
  • Any non-zero value indicates an error.

Examples

Example 1: This program executes a system command to display the current system date. The command runs directly in the operating system shell.

Python
import os
r = os.system("date")
print("Status:", r)

Output
Thu Mar  5 09:32:43 UTC 2026
Status: 0

Explanation: os.system("date") runs the date command in the shell and prints the result. The returned status is stored in r.

Example 2: This example creates a new directory using a system command. It demonstrates how os.system() can execute file system operations.

Python
import os
r = os.system("mkdir test_folder")
print("Status:", r)

Output
Status: 0

Explanation: os.system("mkdir test_folder") runs the directory creation command in the shell. A status of 0 indicates success.

Example 3: This program lists all files and folders in the current directory. It uses a system command executed through os.system().

Python
import os
r = os.system("ls")
print("Status:", r)

Output
Solution.py
driver
input.txt
output.txt
Status: 0

Explanation: os.system("ls") executes the list command in the shell, displaying directory contents. The exit status is stored in r.

Comment