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.
The os.system() function has the following syntax.
- COMMAND can be a command, application, or any executeable.
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 os.system("ls")
As the os.system() can be used for Windows operating system too in the following example we run
import os os.system("dir")
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 os.system("notepad.exe")
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 os.system("/home/ismail/backup.sh")