Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is os.popen really deprecated in Python 2.6?

The on-line documentation states that os.popen is now deprecated. All other deprecated functions duly raise a DeprecationWarning. For instance:

>>> import os
>>> [c.close() for c in os.popen2('ps h -eo pid:1,command')]
__main__:1: DeprecationWarning: os.popen2 is deprecated.  Use the subprocess module.
[None, None]

The function os.popen, on the other hand, completes silently:

>>>len(list(os.popen('ps h -eo pid:1,command')))
202

Without raising a warning. Of the three possible scenarios

  1. It is expected behaviour that documentation and standard library have different ideas of what is deprecated;
  2. There is an error in the documentation and os.popen is not really deprecated;
  3. There is an error in the standard library and os.popen should raise a warning;

which one is the correct one?

For background information, here's the Python I'm using:

>>> import sys
>>> print sys.version
2.6.2 (r262:71600, May 12 2009, 10:57:01) 
[GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)]

The argument to os.popen is taken from a reply of mine here on Stack Overflow.

Addendum: Thanks to cobbal below, it turns out that os.popen is not deprecated in Python 3.1, after all.

like image 585
krawyoti Avatar asked Jul 08 '09 13:07

krawyoti


People also ask

What is OS Popen in Python?

Description. Python method popen() opens a pipe to or from command. The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'. The bufsize argument has the same meaning as in open() function.

How do I close OS Popen in Python?

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 close a “file object” returned by the built-in function open() or by popen() or fdopen() , use its close() method.


1 Answers

Here is the PEP.

Deprecated modules and functions in the standard library:

    - buildtools
    - cfmfile
    - commands.getstatus()
    - macostools.touched()
    - md5
    - MimeWriter
    - mimify
    - popen2, os.popen[234]()
    - posixfile
    - sets
    - sha
like image 58
kjfletch Avatar answered Sep 19 '22 13:09

kjfletch