×

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 reversed() Function

Python reversed() Function The reversed() in Python returns a reverse iterator. Syntax reversed(seq) Parameter seq: This parameter represents any iterable object. Return This function returns a reversed iterator object. Example 1 # Python Program describing # the reversed() function ...

1 minute read.

How to create a DataFrames in Python

How to create a DataFrames in Python Data Frame is a data structure in which data is stored in tabular form. They can also be referred as two-dimensional collection of data. Various...

5 minutes read.

Python ascii() Function

Python ascii() Function The ascii() function returns a readable version of any object (Strings, Tuples, Lists, etc). This function will replace any non-ascii characters with escape characters. Syntax ascii(object) Parameter object:  An object, like String, List, Tuple, Dictionary, etc. Return This...

1 minute read.

Time. Sleep() in Python

Python time sleep () function suspends execution for a certain seconds given by user. Time sleep () syntax: Sleep(seconds) Limitations: The number of seconds to be suspends the code as per its requirements. Returns: Void The execution...

3 minutes read.

Map Syntax in Python

Introduction: In Python, a function called map acts as an iterator, returning a result after each item in an iterable has been subjected to a function (tuple, lists, etc.). When you...

6 minutes read.

Python IDE names

Python is one of the most renowned programming languages. It has different execution conditions and a great many compilers to execute the python programs, e.g., PyCharm, PyDev, Jupyter Notebook, Visual...

6 minutes read.

Image to Text in python

Processing out information from an image is a very big task in all the fields of work such as in development and business sector. The process of converting an electronical...

3 minutes read.

How to Substring a String in Python

String Unicode characters collection is referred to as a String. It consists of a succession of characters, including alphanumeric and special characters. Substring: Core Concept A substring is a piece of a string....

5 minutes read.

Python Fit Transform

In machine learning, fit (), transform(), fit transform () methods are provided by scikit learn package. This package is used in model fitting and data processing. These methods are implemented...

2 minutes read.

Python Printf Style Formating

String objects have one special implicit activity: the % administrator (modulo). This is otherwise called the string designing or interjection administrator. Given design % values (where configuration is a string),...

3 minutes read.

What is Python 2

Python is a widely used high-level language. The initial work on developing python was begun in the late 1980s. In 1989, Guido Van Rossum started to work on it. Initially,...

3 minutes read.

Difference between python list and tuple

In this tutorial, we will understand the key differences between python lists and tuples in python. Firstly, let us see the similarities between Lists and Tuples. Both are two data types...

4 minutes read.

Python List Methods

Python List Methods Python has a set of built-in methods that you can use on lists or arrays. Following are all of the methods of list objects: Methods Explanation append The list.append() method...

3 minutes read.

Array to String in Python

What is an Array: The idea behind an array is to group several items of the same type, making it easier to locate each element by adding an extra offset to...

4 minutes read.

How to Practice Python Programming

Learning python kkis a step towards coding. Python gets one closer to programming languages. It is essential to practice every programming language to become a professional in coding. It is...

4 minutes read.

Python Modulo

For basic calculations, Python provides operators. Python supports a broad range of Arithmetic Operators to do arithmetic, as given below: +Addition*Multiplication-Subtraction/Division//Floor division**Exponentiation%Remainder/Modulus As you can see, one of these basic arithmetic operators...

8 minutes read.

Write Dictionary to CSV in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

4 minutes read.

Exrex Python

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

4 minutes read.

Create a Table 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...

3 minutes read.

Python Set issuperset() method

Python Set issuperset() method The set.issuperset() method in Python returns a boolean value True if all items in the specified set exists in the original set, else it returns False. Syntax set.issuperset (set1) Parameter set- This parameter represents the...

2 minutes read.