在线文档声明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]
Run Code Online (Sandbox Code Playgroud)
另一方面,函数os.popen以静默方式完成:
>>>len(list(os.popen('ps h -eo pid:1,command')))
202
Run Code Online (Sandbox Code Playgroud)
没有提出警告.在三种可能的情况中
哪一个是正确的?
有关背景信息,请参阅我正在使用的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)]
Run Code Online (Sandbox Code Playgroud)
os.popen的参数取自我在Stack Overflow上的回复.
附录:感谢下面的 cobbal ,事实证明os.popen在Python 3.1中并没有被弃用.