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.
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.
What should I do if the command 'os. popen' cannot be read again?
how does the os.popen work()?
python - os.popen().read() - charmap decoding error - Stack Overflow
python - read subprocess stdout line by line - Stack Overflow
Videos
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?
os.popen is just a wrapper around subprocess.Popen along with a io.TextIOWrapper object:
The returned file object reads or writes text strings rather than bytes.
If Python's default encoding doesn't work for you, you should use subprocess.Popen directly.
The underlying issue is that cmd writes ansi garbage by default, even when the output is to a pipe. This behavior may depend on your Windows version.
You can fix this by passing /U flag to cmd:
p = subprocess.Popen('cmd /u /c dir', stdout=subprocess.PIPE)
result = p.communicate()
text = result[0].decode('u16')
In this case, using subprocess.Popen is too general, too verbose and too hard to remember. Use subprocess.check_output instead.
It returns a bytes object, which can be converted to str with decode function.
import subprocess
x = subprocess.check_output(['ls','/'])
print(x.decode('utf-8'))
Try it online!
I think the problem is with the statement for line in proc.stdout, which reads the entire input before iterating over it. The solution is to use readline() instead:
#filters output
import subprocess
proc = subprocess.Popen(['python','fake_utility.py'],stdout=subprocess.PIPE)
while True:
line = proc.stdout.readline()
if not line:
break
#the real code does filtering here
print "test:", line.rstrip()
Of course you still have to deal with the subprocess' buffering.
Note: according to the documentation the solution with an iterator should be equivalent to using readline(), except for the read-ahead buffer, but (or exactly because of this) the proposed change did produce different results for me (Python 2.5 on Windows XP).
Bit late to the party, but was surprised not to see what I think is the simplest solution here:
import io
import subprocess
proc = subprocess.Popen(["prog", "arg"], stdout=subprocess.PIPE)
for line in io.TextIOWrapper(proc.stdout, encoding="utf-8"): # or another encoding
# do something with line
(This requires Python 3.)