os.popen 在 Python 2.6 中真的被弃用了吗?

发布于 2024-07-26 15:54:00 字数 1212 浏览 4 评论 0原文

在线文档指出 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

不会发出警告。 在三种可能的情况中,

  1. 文档和标准库对于弃用的内容有不同的想法,这是预期的行为;
  2. 文档中有一个错误,并且 os.popen 并未真正被弃用;
  3. 标准库中有错误,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

  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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光是把杀猪刀 2024-08-02 15:54:01

这是PEP

标准库中已弃用的模块和函数: 

      - 构建工具 
      -cfm文件 
      - 命令.getstatus() 
      - macostools.touched() 
      -md5 
      - 哑剧作家 
      - 最小化 
      - popen2, os.popen[234]() 
      - posix文件 
      - 套 
      - 沙 
  

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
落在眉间の轻吻 2024-08-02 15:54:01

我能想到的一件事是 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.

岁月如刀 2024-08-02 15:54:01

与此同时,我在 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.

酒解孤独 2024-08-02 15:54:01

commands.getstatusoutput 仍然根据 2.6.4 文档使用它。

commands.getstatusoutput still uses it according to the 2.6.4 documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文