| by Arround The Web | No comments

Python os.system

“Integrating repetitive tasks is a great idea. Shell scripts are frequently used by programmers and software operators to handle some recurring processes. But, shell scripts could get more difficult to manage because these processes get a little more complicated. However, the Python programming language could be utilized for automated processes rather than terminal commands. The same feature as those terminal commands is available in Python through some functions for executing operating system commands.

We can handle computer operations in a systematic and organized manner by acquiring how to execute the command line in Python. The OS package of a python programming language offers tools for cooperating with any operating system of the computer. OS is included in the basic utility packages for Python. This package includes a compact approach of using additional features that rely on the operating system.

The instruction as a string is implemented by the os.system() function in a terminal. The same restrictions apply to this procedure, which would be accomplished by using the system() method of C Programming language. If an instruction produces any outcome, the standard output source of the programmer receives it. When this function is employed, the corresponding operating system terminal is going to open, and the instructions are run there.”

The Attributes of the OS System

A reusable method of integrating with the advanced operating system is provided by the OS system. Along with other functions, it provides access to file contents, local variables, command line parameters, operating parameters, and directory topology. We have been using the same statement that we might utilize in a terminal as the argument of the method in this simplified, Python-based manner.

Utilizing operating system-dependent capabilities is made possible by the Python OS package. We can collaborate with the Programming language operating system by using the methods which the OS package provides.

Parameters Provided to the os.system() Function

The instruction specifies which operation to run. And this instruction will be of the string data type.

Value Returned by the use of the os.system() Method

The operating system will determine how it operates. When we are utilizing UNIX, this will provide the termination status of the program; when we will be utilizing Windows operating system, the resultant value is the outcome of the command which is provided as the parameter to the os.system() function.

In this article, we will explore the numerous execution methods of different commands running on the terminal by using the Python language, as well as the best circumstances through which to apply each one.

As we are using the Windows operating system, we have opened the cmd on the system and then executed the commands of os.system() functions on the command prompt. First, we have to run the command “python”. If our system has installed python, then we get this type of output as shown in the figure below. This output also shows the version of python. If we don’t have python, we get the error on the screen as an output.

Example no 1

In this example, we are going to obtain the existing date of our system. First, we have to integrate the “os” package. Then we initialize a variable “cmd” and set the value “date” to this variable. This is the instruction that is to be executed.

Here we are utilizing the Windows operating system. In the last line, we will be using the system() method. This functionality is linked with the windows operating system. This function has the specified value of the “cmd”. With the help of function os.system(), we will acquire the date of the system in the format of dd/mm/yyyy.

import os

cmd = 'date'

os.system(cmd)

Example no 2

In this instance, we will run the inbuilt component “Notepad” of the system.

import os

cmd = 'notepad'

os.system(cmd)

The “os” module integration must occur first. Afterward, we define the variable “cmd” and assign the value “notepad” to it. This is the instruction that has to be passed out.  The operating system windows are being used here. The system() function could be applied in the final line. The Windows operating system is associated with this method. This method contains the “cmd” value that has been provided. We will access the computer’s notepad on the computer with the aid of the os.system() function.

After executing the above commands, we successfully open the notepad.

Example no 3

Let’s access the chrome in this scenario by using the os.system() function.

import os

os.system("start chrome")

os.system("py")

We will start the program by incorporating the “os” module. Then, after invoking the system() method, we’ll specify the parameter of this function to “start Chrome.” The statement that has to be accomplished would be this. Windows is the operating system being utilized here.  The Windows operating system is related to the function system(). Once again, we will employ the system() method. This function contains the “py” as an argument. Hence we will open Chrome through the use of the os.system() function.

We effectively access Google after running the aforementioned commands.

Conclusion

We have covered the basics of the os.system() in this guide. We have utilized this function in a variety of examples, and the outcomes were also shown. In the first instance, we were using the os.system() method to get the existing date of the system. In a second illustration, the notepad has been opened by using the os.system() function. Similarly, we have called the os.system() method to access Chrome in the last example. The command that we want this function to perform has been supplied to it. The os.system() function will be utilized when we would only have to execute several basic commands and also don’t bother if the outcome appears on the terminal. Python enables the users to run system calls, which are used by the users to better control automated commands or execute related programs.

Share Button

Source: linuxhint.com

Leave a Reply