×

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 for loop

 Python for loop A for loop in Python executes a block of code for a specified number of times, based on a given sequence. The for loop in Python is different than any other...

3 minutes read.

Python Namespace

In python, the namespace is a very important concept that should be understood before using any function or variables. When we write code, we often use variables, libraries, functions, modules, etc....

4 minutes read.

Python String swapcase() method

Python String swapcase() method The string.swapcase() method in Python returns a copy of the string with uppercase characters converted to lowercase and vice versa. Syntax string.swapcase() Parameter NA Return This method returns a copy of the string...

1 minute read.

XXhash Python Examples

XXhash Python: xxHash is an extremely rapid hash calculation that operates inside the confines of RAM. Code is incredibly convenient, and hashes (almost nothing/large endian) are same at all levels. Execution of...

4 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.

What is Python compiler GDB?

The source code of one programming language is converted into machine code, bytecode, or another programming language by a compiler, a specialised software. A compiler is a tool that converts high-level...

3 minutes read.

Python bytearray()

Python bytearray() Class The bytearray() class is used to return a bytearray object which is an array of the specified bytes. It gives a mutable sequence of integers in the range 0...

1 minute read.

GET and POST requests using Python

‘GET’ and ‘POST’ are two request methods of Hypertext Transfer Protocol (HTTP). What is HTTP? HTTP stands for Hypertext Transfer Protocol. It is a collection of protocols which makes communication between client...

4 minutes read.

Python Function

Python Function A Python function is a reusable, organized block of code that is used to perform the specific task. The functions are the appropriate way to divide an extensive program into a...

7 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.

Check Letter in a String Python

Checking a specific letter or a character in a given string or a string which is taken as an input from the user is possible in Python in broadly three...

3 minutes read.

What is the Python Global Interpreter Lock?

Introduction When working with processes, Python employs a form of process lock called the Global Interpreter Lock (GIL). Python typically executes a collection of typed statements using just one thread. It...

4 minutes read.

Python Program to Generate a Random String

The term "random" refers to a group of information or data that can be accessed in any chronological order. To create random strings, a Python program called random is utilized....

6 minutes read.

Python and its advantages

What is Python? Python is an object-oriented programming language, a general-purpose programming language with a high level, and also an interpreted language that has an easy syntax and dynamic semantics. Python...

7 minutes read.

Python for Data Analysis

Data analysis uses various techniques to read, illustrate, manipulate and evaluate a particular data. You can have access to the data and keep the data updated regularly. You can append...

4 minutes read.

Python Super function

The super function is used to access and call the methods or functions involved in the parent class during Inheritance. What is Inheritance? The process where the parent class inherits some of...

3 minutes read.

Python Continue Statement

In Python, loops automate and repeat processes in a cost-effective manner. However, there may be occasions when you wish to entirely exit the loop, skip an iteration, or ignore the...

3 minutes read.

Python program for perfect number

Python program for perfect number Before writing any program for a given problem, we have to understand the problem for which we are creating a solution program. So, let's understand what...

2 minutes read.

Python String index() method

Python String index() method The string.index() method in Python finds the lower index of the first occurrence of the specified value or raise a ValueError if the substring is not found. Syntax index(sub[, start[, end]]) Parameter sub:...

2 minutes read.

Python Set difference_update() method

Python Set difference_update() method The set.difference_update() method in Python removes the items that exist in both sets. Syntax set.difference_update(set1) Parameter set- This argument represents a set (minuend) set1- This arguments represents a set(subtrahend) Return None Example 1 # Python program explaining # the set.difference_update()...

2 minutes read.