How Use Linux Command In Python Using System.Os
Last Updated :
09 Apr, 2024
Using the system
module from the os
library in Python allows you to interact with the Linux command line directly from your Python script. This module provides a way to execute system commands, enabling you to automate various tasks by integrating Linux commands seamlessly into your Python code. Whether it's file manipulation, process control, or system administration tasks, enabling the system
module in Python provides a powerful and efficient way to incorporate Linux commands into your scripts.
Use Linux Command In Python Using os.system
Below are examples of using Linux commands in Python using the os.system function.
Listing Files in a Directory Using System.Os in Python
The below example uses the os.system function to execute the 'ls' command, which is a Linux command for listing files in the current directory. When run, this script prints the names of files and directories present in the script's working directory to the console, providing a quick way to view the contents of the current folder within a Python script.
Python3
import os
# Execute 'ls' command to list files in the current directory
os.system('ls')
Output:
file1.txt file2.txt directory1 directory2
Creating a New Directory Using System.Os in Python
The below example le uses the os.system function to execute the 'mkdir' command, which is a Linux command for creating directories. When run, this script creates a new directory, you can further use the 'ls' command to see the created directory.
Python3
import os
# Execute 'mkdir' command to create a new directory
os.system('mkdir new_directory')
Output:

Checking Disk Space Using System.Os in Python
The below example uses the os.system function to execute the 'df -h' command. When run, this script will display details about the disk space on the partition where the root directory is located.
Python3
import os
# Execute 'df' command to check disk space
os.system('df -h')
Output:

Pinging a host Using System.Os in Python
The below example uses the os.system function to execute the 'ping' command which is a Linux command used to test the reachability of a host. When run, this script will ping the specified host 4 times and display the round-trip time for each packet.
Python3
import os
# Host to ping
host_to_ping = 'google.com'
# Using os.system to execute the 'ping' command
os.system(f'ping -c 4 {host_to_ping}')
Output:

Conclusion
In conclusion, using the os.system function in Python enables seamless integration of Linux commands into scripts, providing a convenient way to automate diverse tasks. The presented examples showcase the versatility of this approach, from listing files and creating directories to checking disk space and pinging hosts. By incorporating Linux commands, Python scripts gain robust functionality for tasks involving file manipulation, system administration, and more.
Similar Reads
Uses of OS and Sys in Python In this article, we will see where we use Os and Sys in Python with the help of code examples. What is Os Module?Python OS module in Python furnishes a versatile means of engaging with the operating system. It facilitates a range of operations, including the creation, deletion, renaming, movement, a
4 min read
Write Os.System Output In File Using Python Python is a high-level programming language. There are many modules. However, we will use os.system module in this Program. This module provides a portable way of using operating system-dependent functionality. The "os" and "os.path()" modules include many functions to interact with the file system.
3 min read
Print Output from Os.System in Python In Python, the os.system() function is often used to execute shell commands from within a script. However, capturing and printing the output of these commands can be a bit tricky. This article will guide you through the process of executing a command using os.system() and printing the resulting valu
3 min read
Get Your System Information - Using Python Script Getting system information for your system can easily be done by the operating system in use, Ubuntu let's say. But won't it be fun to get this System information using Python script? In this article, we will look into various ways to derive your system information using Python. There are two ways t
2 min read
Os Module Vs. Sys Module In Python Python provides a lot of libraries to interact with the development environment. If you need help using the OS and Sys Module in Python, you have landed in the right place. This article covers a detailed explanation of the OS and Sys Module including their comparison. By the end of this article, you
5 min read