AttributeError: '_io.TextIOWrapper' 对象没有属性 'next' python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26967509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:14:01  来源:igfitidea点击:

AttributeError: '_io.TextIOWrapper' object has no attribute 'next' python

python

提问by

I am using python 3.3.3. I am doing the tutorial from tutorialspoint.com. I am unable to understand what this error is.

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

Here is my code:

这是我的代码:

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

Error:

错误:

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'

采纳答案by furkle

There's two reasons you're running into issues here. The first is that you've created foin write-only mode. You need a file object that can read and write. You can also use the withkeyword to automatically destruct a file object after you're done with it, rather than having to worry about closing it manually:

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

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

The second is that (like the error you've pasted very strongly suggests) the file object fo, a text file object, doesn't have a nextmethod. You're using an tutorial written for Python 2.x, but you're using Python 3.x. This isn't going to go well for you. (I believe nextwas/maybe is valid in Python 2.x, but it is not in 3.x.) Rather, what's most analogous to nextin Python 3.x is readline, like so:

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

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

Note that this will only work if the file has at least 7 lines. Otherwise, you'll encounter an exception. A safer, and simpler way of iterating through a text file is with a for loop:

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

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

Or, if you wanted to get a little more pythonic, you could use the enumeratefunction:

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

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

回答by RvdK

You are not following the tutorial correctly. You have opened the file Write onlyopen("foo.txt", "w")

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

The action line = fo.next()is a read, so obviously it will crash. So fix it by opening as write andread: fo = open("foo.txt", "r+")

该操作line = fo.next()是读取,因此显然它会崩溃。因此,通过以写入读取方式打开来修复它:fo = open("foo.txt", "r+")

But that's only for Python 2.7, you should probably use nextor fix the iteration via an other way. Check @furkle's answer.

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

The tutorial is probably also incorrect, see explanation of the modes here: python open built-in function: difference between modes a, a+, w, w+, and r+?

该教程可能也不正确,请参阅此处的模式说明:python open built-in function: difference between mode a, a+, w, w+, and r+?

回答by sinanorz

You can use fo.\__next__()or fo.next()in python3.x.

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