【问题标题】:AttributeError: '_io.TextIOWrapper' object has no attribute 'next' pythonAttributeError:'_io.TextIOWrapper'对象没有属性'next'python
【发布时间】:2014-11-17 07:23:31
【问题描述】:

我正在使用 python 3.3.3。我正在做来自 tutorialspoint.com 的教程。我无法理解这个错误是什么。

这是我的代码:

fo = open("foo.txt", "w")
print ("Name of the file: ", fo.name)

# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line

seq = ["This is 6th line\n", "This is 7th line"]
# Write sequence of lines at the end of the file.
fo.seek(0, 2)
line = fo.writelines( seq )

# Now read complete file from beginning.
fo.seek(0,0)
for index in range(7):
 #  line = fo.next()
   print ("Line No %d - %s" % (index, line)+"\n")

# Close opend file
fo.close()

错误:

Name of the file:  foo.txt
Traceback (most recent call last):
  File "C:/Users/DELL/Desktop/python/s/fyp/filewrite.py", line 19, in <module>
    line = fo.next()
AttributeError: '_io.TextIOWrapper' object has no attribute 'next'

【问题讨论】:

标签: python


【解决方案1】:

您在这里遇到问题有两个原因。首先是您在只写模式下创建了fo。您需要一个可以读写的文件对象。您还可以使用with 关键字在完成后自动销毁文件对象,而不必担心手动关闭它:

# the plus sign means "and write also"
with open("foo.txt", "r+") as fo:
    # do write operations here
    # do read operations here

第二个是(就像您粘贴的错误强烈暗示的那样)文件对象fo,一个文本文件对象,没有next 方法。您正在使用为 Python 2.x 编写的教程,但您使用的是 Python 3.x。这对你来说不会很顺利。 (我相信next 曾经/可能在 Python 2.x 中有效,但在 3.x 中无效。)相反,与 Python 3.x 中的next 最相似的是readline,如下所示:

for index in range(7):
    line = fo.readline()
    print("Line No %d - %s % (index, line) + "\n")

请注意,这仅在文件至少有 7 行时才有效。否则,您将遇到异常。一种更安全、更简单的迭代文本文件的方法是使用 for 循环:

index = 0
for line in file:
    print("Line No %d - %s % (index, line) + "\n")
    index += 1

或者,如果你想获得更多的 Python 风格,你可以使用 enumerate 函数:

for index, line in enumerate(file):
    print("Line No %d - %s % (index, line) + "\n")

【讨论】:

  • 替换 fo.next() 后错误是:回溯(最近一次调用最后一次):文件“C:/Users/DELL/Desktop/python/s/fyp/filewrite.py”,第 20 行, in line = fo.readline() io.UnsupportedOperation: not readable
  • @user3185892 正如该错误所暗示的,您尚未打开可读文件对象。查看我的更新。
【解决方案2】:

您没有正确遵循本教程。您已打开文件只写 open("foo.txt", "w")

动作line = fo.next() 是读取,所以很明显它会崩溃。 所以修复它通过打开写阅读:fo = open("foo.txt", "r+")

但这仅适用于 Python 2.7,您可能应该使用 next 或通过其他方式修复迭代。检查@furkle 的答案。

教程可能也不正确,请看这里的模式解释:python open built-in function: difference between modes a, a+, w, w+, and r+?

【讨论】:

  • 哇塞,我查了一下,应该是r+ stackoverflow.com/questions/10349781/…
  • 这是对的一半——据我所知next() 不是文本文件对象上的有效命令,因此他得到了错误。
  • 没有,我刚试过。 Open() 返回一个可迭代对象。 docs.python.org/2/library/stdtypes.html#iterator.next
  • 另外,如果不是这种情况,它在 forloops 中也不会起作用。
  • 这是 python 2.x 的文档。他和我都在 3.x 上。对我来说,我可以遍历文件对象,但是 .next() 会导致同样的错误。
【解决方案3】:

除了其他人提到的文件模式问题"r+"之外,OP引用的错误消息是因为f.next()方法在Python 3.x中不再可用。它似乎已被内置函数 'next()' (https://docs.python.org/3/library/functions.html#next) 取代,该函数调用迭代器的 .__next__() 方法。

您可以在代码line = fo.__next__() 中的方法中添加下划线,但最好使用内置函数:line = next(fo)

我知道这是一个老问题 - 但我觉得包含这个答案很重要。

【讨论】:

    【解决方案4】:

    你可以在python3.x中使用fo.\__next__()fo.next()

    【讨论】:

      猜你喜欢
      • 2021-05-31
      • 2014-01-16
      • 2015-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多