The os.popen function just returns a file-like object. You can use it like so:

import os

process = os.popen('gcc -E myHeader.h')
preprocessed = process.read()
process.close()

As others have said, you should be using subprocess.Popen. It's designed to be a safer version of os.popen. The Python docs have a section describing how to switch over.

Answer from Brian McKenna on Stack Overflow
🌐
Python
docs.python.org › 3 › library › subprocess.html
subprocess — Subprocess management — Python 3.14.4 documentation
1 week ago - In the following examples, we assume that the relevant functions have already been imported from the subprocess module. ... p1 = Popen(["dmesg"], stdout=PIPE) p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE) p1.stdout.close() # Allow ...
🌐
Tutorialspoint
tutorialspoint.com › python › os_popen.htm
Python os.popen() Method
import os, sys # using command mkdir a = "mkdir nwdir" b = os.popen(a,"r",1) print (b) When we run above program, it will create a directory named "nwdir" with read mode. ... In this example, we are executing the "ls" command which lists the ...
🌐
W3big
w3big.com › python3 › python3-os-popen.html
Python3 os.popen () method - HTML Tutorial
The following example demonstrates popen () methods of use: #!/usr/bin/python3 import os, sys # 使用 mkdir 命令 a = 'mkdir nwdir' b = os.popen(a,'r',1) print (b)
🌐
Stack Abuse
stackabuse.com › pythons-os-and-subprocess-popen-commands
Python's os and subprocess Popen Commands
November 9, 2017 - The bufsize parameter tells popen how much data to buffer, and can assume one of the following values: ... This method is available for Unix and Windows platforms, and has been deprecated since Python version 2.6. If you're currently using this method and want to switch to the Python 3 version, here is the equivalent subprocess version for Python 3: The code below shows an example of how to use the os...
🌐
ProgramCreek
programcreek.com › python › example › 142 › os.popen
Python Examples of os.popen
def get_ip_local(card): if not card != None: get_interface = Refactor.get_interfaces()['activated'] out = popen("ifconfig %s | grep 'Bcast'"%(get_interface)).read().split() for i in out: if search("end",i): if len(out) > 0: ip = out[2].split(":") return ip[0] if len(out) > 0: ip = out[1].split(":") return ip[1] else: out = popen("ifconfig %s | grep 'Bcast'"%(card)).read().split() for i in out: if search("end",i): if len(out) > 0: ip = out[2].split(":") return ip[0] if len(out) > 0: ip = out[1].split(":") return ip[1] return None ... def header(): try: print("\033[34mScanning.... HTTP Header \0
🌐
Cach3
tutorialspoint.com.cach3.com › python3 › os_popen.htm
Python 3 os.popen() Method - Tutorials Point
The following example shows the usage of popen() method. Live Demo · # !/usr/bin/python3 import os, sys # using command mkdir a = 'mkdir nwdir' b = os.popen(a,'r',1) print (b)
🌐
Educative
educative.io › answers › how-to-perform-command-line-interactions-using-ospopen-in-python
How to perform command line interactions using os.popen in Python
Here’s a simple code example showing the use of os.popen(): ... Line 4: Opens a pipe to the "ls" command for reading (specified by the "r" mode). Line 8: Reads and prints the output of the "ls" command. Line 11: Closes the pipe. Despite its simplicity and ease of use, os.popen() has been deprecated in recent versions of Python.
Find elsewhere
🌐
Beautiful Soup
tedboy.github.io › python_stdlib › generated › generated › os.popen3.html
os.popen3() — Python Standard Library
os.popen3() View page source · os.popen3(cmd, mode='t', bufsize=-1)[source]¶ · Execute the shell command ‘cmd’ in a sub-process. On UNIX, ‘cmd’ may be a sequence, in which case arguments will be passed directly to the program without shell intervention (as with os.spawnv()).
🌐
Linux find Examples
queirozf.com › entries › python-3-subprocess-examples
Python 3 Subprocess Examples
November 26, 2022 - # drwxrwxr-x 2 felipe felipe 4,0K Nov 3 19:32 .ipynb_checkpoints # -rw-rw-r-- 1 felipe felipe 5,5K Nov 4 15:28 main.ipynb errors # '' (empty string) import subprocess from subprocess import Popen path_to_output_file = '/tmp/myoutput.txt' myoutput = open(path_to_output_file,'w+') p = Popen(["ls","-lha"], stdout=myoutput, stderr=subprocess.PIPE, universal_newlines=True) output, errors = p.communicate() output # there's nothing here because we didn't set stdout=subprocess.PIPE errors # '' empty string # stdout has been written to this file with open(path_to_output_file,"r") as f: print(f.read()) # total 20K # drwxrwxr-x 3 felipe felipe 4,0K Nov 4 17:00 .
🌐
Python
docs.python.org › 3 › library › os.html
os — Miscellaneous operating system interfaces — Python 3.14.4 documentation
February 23, 2026 - Added in version 3.10. ... Read at most n bytes from file descriptor fd. Return a bytestring containing the bytes read. If the end of the file referred to by fd has been reached, an empty bytes object is returned. ... This function is intended for low-level I/O and must be applied to a file descriptor as returned by os.open() or pipe(). To read a “file object” returned by the built-in function open() or by popen() or fdopen(), or sys.stdin, use its read() or readline() methods.
🌐
Python Module of the Week
pymotw.com › 2 › subprocess
subprocess – Work with additional processes - Python Module of the Week
This example reproduces the command line cat index.rst | grep ".. include" | cut -f 3 -d:, which reads the reStructuredText source file for this section and finds all of the lines that include other files, then prints only the filenames. $ python -u subprocess_pipes.py Included files: subprocess_os_system.py subprocess_shell_variables.py subprocess_check_call.py subprocess_check_output.py subprocess_check_output_error.py subprocess_check_output_error_trap_output.py subprocess_popen_read.py subprocess_popen_write.py subprocess_popen2.py subprocess_popen3.py subprocess_popen4.py subprocess_pipes.py repeater.py interaction.py signal_child.py signal_parent.py subprocess_signal_parent_shell.py subprocess_signal_setsid.py
🌐
Agr0 Hacks Stuff
agrohacksstuff.io › posts › os-commands-with-python
OS Commands with Python | Agr0 Hacks Stuff
September 1, 2023 - In the event that you need to execute some arbitrary OS command from your python script, there are several ways to go about doing it. You can use os.system(), os.popen(), subprocess.run(), and subprocess.Popen(), among others that aren’t worth mentioning. The os module is useful if you have access to a python command environment and want to execute something without caring of the output, most likely because you are kicking back a reverse shell.
🌐
Reddit
reddit.com › r/learnpython › how does the os.popen work()?
r/learnpython on Reddit: how does the os.popen work()?
June 19, 2017 -

The link I found: https://www.tutorialspoint.com/python/os_popen.htm, tells me that os.popen works like a pipe, but my book gives me a very confusing example:
1) open('something.py')

which is because we need an open stream. next it gives:

  h = os.popen('type something.py').read()

At this point, I'm not getting, if os.popen() acts like a pipe('|' on the terminal), then how is this supposed to be pipeing to the stream opened. and what is it piping?

🌐
Google Sites
sites.google.com › site › pythonpasapas › modules › os › os-popen
Mon Python pas à pas - os.popen ( )
La méthode os.popen ( ) permet d'exécuter une commande système depuis un script Python et de dévier sa sortie dans un objet fichier, à la place de l'écran de la console, pour consulter le résultat ou interagir avec lui selon la valeur de l'attribut mode spécifié.
🌐
Python
docs.python.org › 3 › library › os.html
os — Miscellaneous operating system interfaces — Python 3.14.2 documentation
Added in version 3.10. ... Read at most n bytes from file descriptor fd. Return a bytestring containing the bytes read. If the end of the file referred to by fd has been reached, an empty bytes object is returned. ... This function is intended for low-level I/O and must be applied to a file descriptor as returned by os.open() or pipe(). To read a “file object” returned by the built-in function open() or by popen() or fdopen(), or sys.stdin, use its read() or readline() methods.
🌐
Python
docs.python.org › 3 › library › os.html
os — Miscellaneous operating system interfaces — Python 3.14.0 documentation
Added in version 3.10. ... Read at most n bytes from file descriptor fd. Return a bytestring containing the bytes read. If the end of the file referred to by fd has been reached, an empty bytes object is returned. ... This function is intended for low-level I/O and must be applied to a file descriptor as returned by os.open() or pipe(). To read a “file object” returned by the built-in function open() or by popen() or fdopen(), or sys.stdin, use its read() or readline() methods.
🌐
DataCamp
datacamp.com › tutorial › python-subprocess
An Introduction to Python Subprocess: Basics and Examples | DataCamp
September 12, 2025 - In this example, the Popen class is used to create two child processes, one for the ls command and one for the grep command. The stdout of the ls command is connected to the stdin of the grep command using subprocess.PIPE, which creates a pipe between the two processes.
🌐
Python Forum
python-forum.io › thread-13169.html
os.popen output to a list ..
October 2, 2018 - hey all good morning .... im trying to run this command on my system that get my running process in the system import os d = os.popen('''wmic process get name | findstr /v 'Name 'System Idle Process' System''').read().strip() print d #q = ['chro...