在python 2.6中os.popen真的被弃用了吗? [英] Is os.popen really deprecated in Python 2.6?

查看:261
本文介绍了在python 2.6中os.popen真的被弃用了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在线文档指出os.popen是现在已弃用。所有其他不推荐使用的功能会适当引发DeprecationWarning。例如:

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]

另一方面,函数os.popen完成

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. 预期的行为是文档和标准库对不推荐使用的内容有不同的想法;

  2. 文档中有错误,并且os.popen并没有真正被弃用;

  3. 标准库中有错误,并且os.popen应该发出警告;

哪个是正确的?

有关背景信息,以下是我正在使用的Python:

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)]

os.popen的参数来自在堆栈溢出上回复我的

The argument to os.popen is taken from a reply of mine here on Stack Overflow.

附录:感谢cobbal ,事实证明os.popen是毕竟在Python 3.1中不推荐使用。

Addendum: Thanks to cobbal below, it turns out that os.popen is not deprecated in Python 3.1, after all.

推荐答案

这是 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


这篇关于在python 2.6中os.popen真的被弃用了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆