Skip to content
Related Articles

Related Articles

Improve Article

Executing Shell Commands with Python

  • Last Updated : 01 Oct, 2020
Geek Week

Prerequisite: Some basic shell commands

In programming, the shell is a software interface for accessing the functionality of the operating system. Shells in the operating system use either a CLI (Command Line Interface) or a GUI (Graphical User Interface) based on the functionality and basic operation of the device.

The os module in Python includes functionality to communicate with the operating system. The os module is one of the standard utility modules of Python. This Python module offers a convenient way to use operating system-dependent features, shell commands can be executed using the system() method in the os module.

Below are some examples to execute shell commands using Python:

Example 1:



Python3




# Importing required module
import os
  
# Using system() method to 
# execute shell commands
os.system('echo "Geeks 4 Geeks"')

Output:

Example 2: 

Python3




# Importing required module
import os
  
# Using system() method to execute
# shell commands
os.system('pwd')

Output: 

Example 3: 



Python3




# Importing required module
import os
  
# Using system() method to execute
# shell commands
os.system('ls')

Output: 

Example 4: 

Python3




# Importing required module
import os
  
# Using system() method to execute
# shell commands
os.system('cat')

Output: 

Example 5: 

Python3




# Importing required module
import os
  
# Using system() method to execute
# shell commands
os.system('telnet towel.blinkenlights.nl')

Output: 
 

 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :