×

Python System Command

To execute a program in Python, we need to execute some shell commands to run our program on the computer. Python will provide some shell commands in our background to run the program. Let us consider executing the LIME module in Python; for this, we need to execute some command lines; these commands will interact with the operating system and run the program.

Generally, a python programming language will provide us with the OS module, which will provide us with the functions that will interact with the operating system in our computer and help us run the program. The OS module will fall under Python's standard utility module category. These shell commands will help automate the computer tasks to run the program in a structured and scalable manner.

Python os.system( ) Method

The python os.system( ) function or method helps execute the command in the subshell. This function will be implemented by calling the Standard C function system ( ) with the same limitations. After running this function, if the command generates any output, it will be sent to the interpreter, and we can visualize the output. This Os.system( ) function runs in the following way, when we call this function, it will directly open the corresponding shell in the operating system, and then the command is executed on the respective shell.

Syntax:

os.system(command)

Parameters:

We will give the command as an input, a string data type; this command will help the operating system execute.

Return:

In UNIX, the return value will be the program's exit status, and on Windows, the return value will be exit values obtained from the system shells after running the command.

Example:

Let us consider an example of executing a command in the os.system( ) function, which will return the current day.

Code:

#importing the libraries
importos
# Adding the command value
#With the help of the windows os command


command = ‘day’


#Calling the os.system( ) method
os.system(command) 

Output:

The current day is: Saturday

Example:

Let us consider another example of running the command to open the notepad in the computer with the help of the os.system( ) function.

Code:

#importing the module
importos
#Getting the command value which must be executed
# Running the Windows OS command
command = ‘Notepad’
# Calling the os.system( ) function
os.system(command)

Output:

Python System Command

Example:

Let us consider an example of executing a command in the os.system( ) function, which will return the current date.

Code:

#importing the libraries
importos
# Adding the command value
#With the help of the windows os command
command = ‘date’
#Calling the os.system( ) method
os.system(command) 

Output:

The current date is: Saturday, 1-10-2022

Python subprocess.call() Module:

The python subprocess.call ( ) function or module is the same as theos.system( ) function, but after running this function, the value obtained at the output is stored in some variable.

The subprocess module is the updated version of the os.system( ) function. Nowadays, the subprocess.call( ) function is used for all the shell process rather than the os.system( ) function.

Example:

 Let us consider an example of executing a command in the os.system( ) function, which will return the current day.

Code:

#importing the libraries
importos
# Adding the command value
#With the help of the windows os command


command = ‘day’


#Calling the subprocess.call( ) method
return_output = subprocess.output(command) 
# Calling the decode( ) function to convert the byte string into the string 
print( 'The Current day is:', return_output.decode(“utf))

Output:

The Current day is: Saturday

Example:

Let us consider another example of running the command to open the notepad in the computer with the help of the os.system( ) function.

Code:

#importing the module
importos
#Getting the command value which must be executed
# Running the Windows OS command
command = ‘Notepad’
# Calling the subprocess.call( ) function
output= subprocess.return(command)


#Displaying the output
print(output)

Output:

Python System Command

Example:

Let us consider an example of executing a command in the os.system( ) function, which will return the current date.

Code:

#importing the libraries
importos
# Adding the command value
#With the help of the windows os command
command = ‘date’


#Calling the Subprocess.call( ) method
return_value= subprocess.output(command) 
#Converting the byte string into the string using the decode function
print('The Current date is:’, return_output.decode(“utf)

Output:

The current date is: Saturday, 1-10-2022

Related Topics

Python Debugger

In this tutorial, we will understand what is debugging in general. Then we will realize what the python debugger means and what is the method to perform it. Now, let us...

3 minutes read.

NSE Tools In Python

About NSE NSE (National Stock Exchange) of India Limited is the advanced stock exchange of India. It is located in Mumbai, Maharastra and It was organized in 1992. It was...

2 minutes read.

How to plot a Histogram in Python

A Histogram is used to represent a given data provided as a chunk. This histogram is a graphical representation that uses bars to indicate the ranges of the data. In...

4 minutes read.

How to set up a proxy using selenium in python

What is Proxy? A proxy acts as a middleman between user requests and server responses. The main purposes of proxies are to protect user privacy and encapsulate data between various interactive...

3 minutes read.

Expressions in Python

What is Expression in Python? The expression contains more than one operator as well as the operands with it. Expression helps us to produce some other values. In the Python programming...

12 minutes read.

Python globals() function

Python globals() function The globals() function in Python returns the value of the named attribute of object where the ‘name’ must be a string. Syntax globals() Parameter NA Return This function returns the global symbol table as a dictionary. Example...

1 minute read.

Find Last Occurrence of Substring using Python

Introduction When planning to work with strings, we may need to determine whether a substring is present. This issue is rather typical, and there have been numerous discussions about how to...

3 minutes read.

Difference Between Python and Scala

What is Python? Python is a high-level general-purpose interpreted programing language. It is used for multi general-purpose work such as language construct as well as its object-oriented approach aims to help...

4 minutes read.

How to read data from com port in python

Comport, the I/O interface is known as a COM port that allows the connection for a serial device to a computer. COM ports are sometimes referred to as serial ports....

3 minutes read.

Difference between For Loop and While Loop in Python

What is the “For” loop? The “For” Loop is a commonly used control structure in programming that allows us to repeat a set of actions multiple times. The “For” loop is...

3 minutes read.

Python Linked List

Linked List Linked lists non-contiguous linear data structure which is made up of nodes and used to store value and a pointer pointing to the next node. It is linked with...

6 minutes read.

Python String splitlines() method

Python String splitlines() method The string.splitlines() method in Python splits the specified string and returns a list of the lines in the string, breaking at line boundaries.  Syntax splitlines([keepends]) Parameter keepends(optional): This parameter specifies if...

1 minute read.

How to reverse a string in Python

In Python language, we have a few ways to reverse a string. In this article, let us discuss these ways and understand the suitability of these ways in different scenarios...

5 minutes read.

Function annotations() in Python

What is Function Annotations? Function annotations provide a way related to different parts of a function at compile time with arbitrary Python expressions. It doesn’t have life in Python runtime execution....

3 minutes read.

Python complex() class

Python complex() class The complex() class in Python returns a complex number or converts a string or number to a complex number. Syntax class complex([real[, imag]]) Parameter Real: This parameter consists of a number representing the real...

1 minute read.

Cross Entropy in Python

Introduction Cross-entropy loss is frequently combined with the softmax function. Determine the total entropy among the distributions or the cross-entropy, which is the difference between two probability distributions. For the purpose...

5 minutes read.

Write Text files in Python

Let's discuss writing the text file in detail. Generally, writing text also follows the same procedure as like reading text. It varies only in some specific places. Steps followed for writing...

4 minutes read.

How To Take Multiple Inputs In Python

In C language, we make use of the scanf() function to obtain the values from the user and store it in the variable. Coming to the Python language, we use the...

5 minutes read.

Colors in Python

Adding colour to your visualisations will help them come to life. Even if you know the colours you want to use, picking good ones and putting them into practise might...

4 minutes read.

Weight Conversion GUI using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

3 minutes read.