Python os.system() Method Example
The os.system() method in Python is simple to use and interpret: use as an input of the function the same command you would use in a shell. Python OS module presents a way of using operating system dependent functionality. The functions that the OS module provides allow you to interact with the underlying operating system that Python is running on for example that can be Windows, Mac, or Linux.
Python os.system() Method
Python os.system() is an inbuilt method that executes the command in the subshell. The os.system() method is implemented by calling the Standard C function system() and has the same limitations.
The command is of string type that tells which command to execute.
It is based on the Operating system you are using. If you are using UNIX, it will return the exit status of the process, and if you are using Windows, then the return value is the value returned by the system shell after running the command.
The os.system() function will execute the shell command, and the result will be printed to the standard output, but the output that the function returns is a return value.
We will use the os.system() method to get the current date of the computer.
import os cmd = 'date' # Using os.system() method os.system(cmd)
Fri Jan 15 15:03:31 IST 2021
Let’s try to know the version of git using the system command git –version.
import os cmd = "git --version" # Using os.system() method os.system(cmd)
git version 2.24.3 (Apple Git-128)
Notice that we are not printing the git version command output to the console; it’s being printed because the console is the standard output stream here.
That is it for the Python os.system() method.