Python文档:os.popen
:
打开或发出命令的管道。返回值是连接到管道的打开文件对象,可以根据模式是“r”(默认)还是“w”来读取或写入。
我可以使用next
方法X.__next__()
/ X.next()
(2.X)但不能使用next(x)
来电,
__next__
方法而next(x)
方法相同吗? next(x)
用于os.popen
的对象?最后但同样重要的是,next()
和next
方法的确如何运作?
答案 0 :(得分:5)
查看源代码(Python 3.4)似乎__next__
类中没有实现_wrap_close
方法,因此next()
调用失败,因为它找不到{{1}类上的方法。明确的__next__
调用因为被覆盖的__next__
方法而有效。
相关source code:
__getattr__
答案 1 :(得分:0)
https://docs.python.org/2/library/functions.html#next说next
:
通过调用next()方法从迭代器中检索下一个项目。如果给定default,则在迭代器耗尽时返回,否则引发StopIteration。
错误消息:
TypeError: _wrap_close object is not an iterator
表示它不是迭代器。很可能缺少__iter__
方法。
您收到错误很奇怪,因为这在Python 2.x中适用于我:
>>> next(os.popen('ls'))
'foo.txt\n'