Print Output from Os.System in Python
Last Updated :
28 Apr, 2025
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 values.
How to Print Value that Comes from Os.System in Python?
Below is the step-by-step guide of How To Print Value That Comes From Os. System in Python:
Step 1: Import the os Module
Before using the os.system()
function, you need to import the os
module in your Python script. This module provides a way to interact with the operating system, including executing shell commands.
import os
Step 2: Use os. system() to Execute Command
The os.system()
function takes a string argument, which is the shell command you want to execute. For example, let's execute the ls
command in a Unix-like system to list the files in the current directory:
os.system("ls")
This command will print the output directly to the console.
Step 3: Capture the Output
To capture the output of the command and print it in your Python script, you can use the subprocess
module. The subprocess
module provides more control and flexibility over executing and interacting with external processes. In this example, the subprocess.check_output()
function is used to capture the output of the command. The text=True
argument ensures that the result is returned as a string.
Python3
import subprocess
command = "ls"
result = subprocess.check_output(command, shell=True, text=True)
print(result)
Output:
sample_data
Step 4: Handling Exit Status
It's important to note that the os.system()
and subprocess
.check_output()
functions return the exit status of the executed command. A non-zero exit status usually indicates an error. You can check and handle the exit status in your script. This way, you can catch and handle errors gracefully in case the executed command fails.
Python3
import subprocess
command = "ls"
try:
result = subprocess.check_output(command, shell=True, text=True)
print(result)
except subprocess.CalledProcessError as e:
print(f"Error executing command: {e}")
Output:
sample_data
Code Examples
Example 1: In this Python example, the os.system
function is used to run a simple command (echo Hello, World!
). The os.popen
function is then used to capture the output of the command, and the result is printed.
Python3
import os
# Run a command using os.system
command = "echo Hello, World!"
os.system(command)
# Capture the output of the command using subprocess
result = os.popen(command).read()
# Print the result
print("Output from command:", result)
Output:
Output from command: Hello, World!
Example 2: In this Python example, the os.system
function is used to run a command (ls -l
to list files in the current directory). The exit code of the command is captured, and it is printed to the console. The exit code can provide information about the success or failure of the executed command.
Python3
import os
# Run a command using os.system
command = "ls -l" # Example command to list files in the current directory
os.system(command)
# Capture the exit code of the command
exit_code = os.system(command)
# Print the exit code
print("Exit code:", exit_code)
Output:
Exit code: 0
Conclusion
In conlcusion, Printing values that come from os.system()
in Python involves using the subprocess
module to capture the output of the command. This approach gives you more control and allows for better error handling. By following the steps outlined in this guide, you can effectively execute shell commands and incorporate their output into your Python scripts.
Similar Reads
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
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
How Use Linux Command In Python Using System.Os 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. Whe
3 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
How to Print without newline in Python? In Python, the print() function adds a newline by default after each output. To print without a newline, you can use the end parameter in the print() function and set it to an empty string or a space, depending on your needs. This allows you to print on the same line. Example:Pythonprint("geeks", en
3 min read