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
- It is expected behaviour that documentation and standard library have different ideas of what is deprecated;
- There is an error in the documentation and os.popen is not really deprecated;
- 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.
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
one thing that I can think of is that os.popen exists in python3, while os.popen2 doesn't. So one is "more deprecated" than the other, and scheduled for sooner removal from the language.
In the meanwhile I have opened a ticket on the Python issue tracker. I'll keep this question open until the ticket is closed.
commands.getstatusoutput still uses it according to the 2.6.4 documentation.
来源:https://stackoverflow.com/questions/1098257/is-os-popen-really-deprecated-in-python-2-6