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

How to store os.system() output in a variable or a list in python - Stack Overflow
I am trying to get the output of a command by doing ssh on a remote server using below command. os.system('ssh user@host " ksh .profile; cd dir; find . -type f |wc -l"') Output of this command is ... 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
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
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
🌐
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 › 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 ...
Find elsewhere
🌐
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]
🌐
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
🌐
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.
🌐
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)
🌐
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 ...
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-system-command-os-subprocess-call
Python os.system() vs subprocess: Run System Commands | DigitalOcean
March 17, 2026 - There are a few things to keep in mind with os.system(): No output capture. The command’s output goes directly to the console. You cannot store it in a variable.
🌐
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...