How To Run OS Command or Program In Python?

Python provides the os module for the operating system-related functions. The os module can be also used to run a command and program via the Python code. The os.system() function runs or executes provided strings as commands for the current operating system. This method works for Linux, Windows, and MacOS operating systems without a problem.

os.system() Syntax

The os.system() function has the following syntax.

  • COMMAND can be a command, application, or any executeable.

Run Command

The os.system() can be used to run commands for different operating systems. In the following example, we run the ls command for Linux.

import os


As the os.system() can be used for Windows operating system too in the following example we run dir command.

import os


Start Application

The system.os() can be also used to start an application. The application executable name should be provided as a command. In the following example, we start the notepad.exe executable by using the os.system() .

import os


Run Command with Absolute (Complete) Path

A command or application can be also executed by providing it with an absolute or complete path. In the following example, we run a bash script which is located under the user home directory.

import os


Leave a Comment