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.
python - reading a os.popen(command) into a string - Stack Overflow
how does the os.popen work()?
How to use subprocess popen Python - Stack Overflow
python - Is there a problem with using deprecated os.popen? - Raspberry Pi Stack Exchange
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.
import subprocess
p = subprocess.Popen('gcc -E myHeader.h'.split(),
stdout=subprocess.PIPE)
preprocessed, _ = p.communicate()
String preprocessed now has the preprocessed source you require -- and you've used the "right" (modern) way to shell to a subprocess, rather than old not-so-liked-anymore os.popen.
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?
subprocess.Popen takes a list of arguments:
from subprocess import Popen, PIPE
process = Popen(['swfdump', '/tmp/filename.swf', '-d'], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
There's even a section of the documentation devoted to helping users migrate from os.popen to subprocess.
In the recent Python version, subprocess has a big change. It offers a brand-new class Popen to handle os.popen1|2|3|4.
The new subprocess.Popen()
import subprocess
subprocess.Popen('ls -la', shell=True)
Its arguments:
subprocess.Popen(args,
bufsize=0,
executable=None,
stdin=None, stdout=None, stderr=None,
preexec_fn=None, close_fds=False,
shell=False,
cwd=None, env=None,
universal_newlines=False,
startupinfo=None,
creationflags=0)
Simply put, the new Popen includes all the features which were split into 4 separate old popen.
The old popen:
Method Arguments
popen stdout
popen2 stdin, stdout
popen3 stdin, stdout, stderr
popen4 stdin, stdout and stderr
You could get more information in Stack Abuse - Robert Robinson. Thank him for his devotion.