Home > Python > Using subprocess for popen

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
About these ads
Categories: Python Tags: , ,
  1. June 17, 2010 at 6:48 am

    I will give it a try on my computer when I get home.
    Nice tip!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: