os.popen 在 Python 2.6 中真的被弃用了吗?
在线文档指出 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 的参数取自我的回复< /a> 位于 Stack Overflow 上。
附录:感谢cobbal 下面,事实证明 os.popen 在 Python 3.1 中并没有被弃用。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是PEP。
Here is the PEP.
我能想到的一件事是 os.popen 存在于 python3 中,而 os.popen2 则不存在。 因此,其中一种比另一种“更不受欢迎”,并计划尽快从该语言中删除。
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.
与此同时,我在 Python 问题跟踪器上开了一张票。 我将保留这个问题,直到票证关闭为止。
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 仍然根据 2.6.4 文档使用它。
commands.getstatusoutput still uses it according to the 2.6.4 documentation.