From this question which I asked a long time ago, what you may want to use is popen:

os.popen('cat /etc/services').read()

From the docs for Python 3.6,

This is implemented using subprocess.Popen; see that class’s documentation for more powerful ways to manage and communicate with subprocesses.


Here's the corresponding code for subprocess:

import subprocess

proc = subprocess.Popen(["cat", "/etc/services"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print("program output:", out)
Answer from Chris Bunch on Stack Overflow
Discussions

python - How to store output of os.system() in a variable - Stack Overflow
The os.system return the exit code of the command. To capture the output of the command, you can use subprocess.check_output More on stackoverflow.com
🌐 stackoverflow.com
linux - reading output into variables inside the os.system command - Unix & Linux Stack Exchange
Suggest me on how to store the variables using os.system command how to use them in the later stages. My target here is to print all the variables including the time being captured into a output file outputfile.txt. More on unix.stackexchange.com
🌐 unix.stackexchange.com
February 8, 2018
how to get the stdout of the script that os.system executes - Post.Byes
The only way I've been able to get output from os.system is to use ">" in the command to make the subshell write its output to a file. Then all I have to do is read the file. More on post.bytes.com
🌐 post.bytes.com
October 31, 2007
Python: How to get stdout after running os.system? - Stack Overflow
I want to get the stdout in a variable after running the os.system call. ... result will contain the error code (stderr 0 under Windows or 1 under some linux for the above example). How can I get the stdout for the above command without using redirection in the executed command? ... By using the subprocess module instead. os.system does not give you access to process input or output... More on stackoverflow.com
🌐 stackoverflow.com
October 13, 2014
🌐
Medium
pramodgupta12378.medium.com › mastering-python-storing-command-output-in-variables-419e030c0f43
Mastering Python: Storing Command Output in Variables | by Pramod Kumar Gupta | Medium
July 17, 2023 - import os # Replace 'your_command_here' ... commands and capture their output. The subprocess.run() function allows you to run a command and store its output in a variable....
🌐
Quora
quora.com › How-do-i-get-os-system-to-say-a-variable
How do i get os.system() to say a variable? - Quora
Answer (1 of 4): I have no useful knowledge of macOS. In case you’re interested in MS Windows/DOS I offer the following. os.system returns only a completion code. A DOS box will flash on the screen during execution. If you want environment variables then you might be best to use os.environ whic...
🌐
CopyProgramming
copyprogramming.com › howto › assign-output-of-os-system-to-a-variable-and-prevent-it-from-being-displayed-on-the-screen-duplicate
Python: Preventing os.system output from displaying on screen by assigning it to a variable [duplicate]
May 11, 2023 - I would like to assign command output to the variable and prevent it from being displayed on the screen. var = os.system("cat /etc/services") print var #Prints 0
Find elsewhere
🌐
CopyProgramming
copyprogramming.com › howto › how-to-store-os-system-output-in-a-variable
Capturing Python OS System Output to a Variable: Complete Guide for 2026
November 18, 2025 - The os.system() function executes a shell command directly but has significant limitations. It returns only the exit code (0 for success, non-zero for failure), not the actual command output. The command's output prints directly to the console without being stored in a variable, making it ...
🌐
Post.Byes
post.bytes.com › home › forum › topic › python
how to get the stdout of the script that os.system executes - Post.Byes
October 31, 2007 - If i use "if os.system(cmd) != 0:" to check it before running "var=commands.g etoutput(cmd)", then the stdout will not only be written to variable var, but also pop on the prompt screen, which is not what I like.. Does anyone have some idea? commands.getsta tusoutput [code=python] >>> fail = commands.getsta tusoutput("ps -q") >>> notfail = commands.getsta tusoutput("ps -A") >>> fail[0] 256 >>> len(fail[1]) # the output 1517 >>> notfail[0] 0 >>> len(notfail[1]) # the output 3408 [/code]
🌐
Medium
shivrajan.medium.com › assign-output-of-os-system-to-a-variable-and-prevent-it-from-being-displayed-on-the-screen-95392dfdd5fd
Assign output of os.system to a variable and prevent it from being displayed on the screen | by Shivrajan Singh | Medium
March 25, 2023 - def out(command): result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=True) return result.stdoutmy_output = out("echo hello world") print(my_output)
🌐
Ubuntu Forums
ubuntuforums.org › archive › index.php › t-1777172.html
Set output of os.system('command') to variable? [Archive] - Ubuntu Forums
June 7, 2011 - system returns the exit code of the child process not the output. use subprocess Popen: p = subprocess.Popen(["echo", "bla"], stdout=subprocess.PIPE, stderr=subprocess.PIPE); stdout, stderr = p.communicate() or if you have python 2.7 you can use the convinience function check_output: stdout = subprocess.check_output(["echo", "bla"]) ... os.system() is the worst option to interface with another program, exactly because of the behaivor you're experiencing: it runs in a new shell and all it returns is the exit code.
🌐
GeeksforGeeks
geeksforgeeks.org › python › write-os-system-output-in-file-using-python
Write Os.System Output In File Using Python - GeeksforGeeks
July 23, 2025 - The below Python code uses os.system() to execute the "echo" command, redirecting its standard output (stdout) to a file named "output.txt" using the "1>" operator.
🌐
MathWorks
mathworks.com › matlab › environment and settings › system commands
system - Execute operating system command and return output - MATLAB
The function starts a new cmd/shell process, executes command, exits the process, and returns to the MATLAB® process. Updates to the system environment made by command are not visible to MATLAB. [status,cmdout] = system(command) also returns the output of the command ...
🌐
Reddit
reddit.com › r/learnpython › on linux how is this os.system return a type "int"
r/learnpython on Reddit: On linux how is this os.system return a type "int"
October 8, 2018 - top = subprocess.check_output("top -n 1 -b") top.stdout #this should be your output. ... import os top = str(os.system("top -n 1 -b")) with open('test.txt', 'w') as write_obj: write_obj.write(top)
🌐
Python Forum
python-forum.io › thread-2239.html
os.system Output
So a new one for me. Any help appreciated. I would no doubt be able to find the answer eventually, but its nice to talk to other humans from time to time. I am trying to query AD at a point in my code, to check to see if a host exists on the doma...
🌐
nixCraft
cyberciti.biz › nixcraft › howto › python › python run external command and get output on screen or in variable
Python Run External Command And Get Output On Screen or In Variable - nixCraft
March 28, 2023 - For example, I would like to call ... in a variable. How do I display such output and exit status (return code) of /bin/date program using a Python script? You need to use the subprocess Python module which allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. Another option is to use operating system interfaces ...