在线文档指出os.popen现已被弃用。所有其他弃用的函数都会相应地引发DeprecationWarning警告。例如:
>>> 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]
另一方面,函数os.popen则默默地完成:
>>>len(list(os.popen('ps h -eo pid:1,command')))
202
不发出警告。三种可能的情况如下:
- 文档和标准库对于何为弃用有不同的理解,这是预期行为;
- 文档存在错误,os.popen并非真正被弃用;
- 标准库存在错误,os.popen应该发出警告;
哪个是正确的?
背景信息:我正在使用的Python版本如下:
>>> 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)]
os.popen的参数来自我在Stack Overflow上的回复。