×

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

How to Install Tweepy in Python

In this article, we will learn or understand how to install Tweepy in Python and what Tweepy is. Firstly, let’s understand What Tweepy is. As we all know, one of the...

3 minutes read.

Python Set add() Method

Python Set add() Method The set.add() method adds the specified element to a set. If the element is already present in the set, it doesn't add it. Syntax set.add(element) Parameter element- This parameter represents the element that...

1 minute read.

An Introduction to Mocking in Python

Python Mocking is a testing library. It enables you to substitute portions of your system under test with fake(mock) objects and make assertions about how they were utilized.  The test is...

3 minutes read.

SKLearn Linear Module

The SK learn linear module is one such module that helps to study the relationship between the independent and dependent variables.The linear module can be implemented by using the best...

3 minutes read.

What is the re.sub() function in Python

There. sub () function is used to return a string by replacing the occurrences of a specific character or pattern with a replacement string. To use this function, import the...

3 minutes read.

Nested List in Python

The list is an inbuilt sequential data type in Python. It is a very useful and frequently used data type. A list can store any number of data items of...

4 minutes read.

Classes & Objects in Python

Classes and Objects are used in most modern programming languages, mainly in Object-oriented Programming languages. What is meant by Object Oriented Programming language? Usage of objects and their components in order to...

7 minutes read.

Application to get live USD/INR rate Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.

Difference between Input() and raw_input() functions in Python

There are two input functions in Python that are used for taking input are given below: raw_input()input() What is raw_input() Function? raw_input() function is a built-in function in Python. It is used to...

6 minutes read.

Hog Descriptor Opencv Python

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

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

Pointers in Python

In this tutorial, we will study what pointers are and if they have any utility in python. Now, let us understand what pointers are Pointers Pointers are special variables used to store the...

3 minutes read.

Python List copy() method

Python List copy () method The list.copy () method returns a shallow copy of the list. Syntax list.copy() Parameter NA Return This function returns the copy for the given list. Example 1 # Python program explaining # the list.copy() method # passing the...

1 minute read.

Python List clear() method

Python List clear() method The list.clear () method in Python extends the list by appending all the items from the iterable. Syntax list.clear() Parameter NA Example 1 # Python Program explaining # the list.clear() method week_list = ["Monday", "Tuesday", "Wednesday","Thursday","Friday",...

1 minute read.

How to change the names of Columns in Python

Introduction To play with huge amounts of data, in Python we require a tool. The tool which is available in Python is Pandas. Pandas is an open-source library. It is used...

3 minutes read.

Python Switch Case

What is a Switch Case Statement? In the programming languages, a switch statement is a logical loop or syntax that tests the variable's value and compares it with multiple cases. Once...

3 minutes read.

Python Tuple Methods

Python Tuple Methods Python has two built-in methods that are used for tuples. The following are the two methods: Method Description count() The tuple.count() method in Python returns the number of times a...

1 minute read.

Python logging Module

Python logging Module Introduction By logging word we understand the tracking of the events which happens when we run some software. Logging process is very important part for developing software, debugging...

12 minutes read.

Python Unit Test Cheat String

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python Parse Text File

We will learn different ways of read text records in Python. TL;DR The accompanying tells the best way to read all texts from the readme.txt document into a string: with open('readme.txt') as f: lines...

5 minutes read.