Open In App

How Use Linux Command In Python Using System.Os

Last Updated : 09 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Creating New Directory

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:

Checking Disk Space

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:

Pinging a Host

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads