Using subprocess for popen
While trying to use a Python script today I came across this:
/usr/local/lib/python2.6/site-packages/londiste/repair.py:73: DeprecationWarning: os.popen4 is
deprecated. Use the subprocess module.
s_in , s_out = os.popen4("sort –version")
The troubling code:
s_in, s_out = os.popen4("sort --version")
Now this is because with Python 2.6 and above ‘popen4′ is something that is a depreciated feature, which means it will be better to change it to use ‘subprocess’ in order to get rid of the warning message.
A replacement to that is by using ‘subprocess’ as…
p = subprocess.Popen("sort --version", shell=True, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
(s_in, s_out) = (p.stdin, p.stdout)
For using subprocess, you will have to add this as well:
import subprocess
Using subprocess instead of popen does the same thing and gets rid of the annoying warning.
–
Shoaib Mir
shoaibmir[@]gmail.com
import subprocess
Categories: Python
popen, Python, subprocess
I will give it a try on my computer when I get home.
Nice tip!