29

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

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'
Mike McKerns
  • 33,715
  • 8
  • 119
  • 139

4 Answers4

39

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

# 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 next method. 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 next was/maybe is valid in Python 2.x, but it is not in 3.x.) Rather, what's most analogous to next in Python 3.x is readline, like so:

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:

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 enumerate function:

for index, line in enumerate(file):
    print("Line No %d - %s % (index, line) + "\n")
furkle
  • 5,019
  • 1
  • 15
  • 24
  • after replacing fo.next() error is : Traceback (most recent call last): File "C:/Users/DELL/Desktop/python/s/fyp/filewrite.py", line 20, in line = fo.readline() io.UnsupportedOperation: not readable –  Nov 17 '14 at 07:35
  • @user3185892 As that error suggests, you haven't opened a readable file object. See my update. – furkle Nov 17 '14 at 07:48
5

In Addition to the file mode issue "r+" which others have mentioned, the error message quoted by the OP happens because the f.next() method is no longer available in Python 3.x. It appears to have been replaced by the built in function 'next()' (https://docs.python.org/3/library/functions.html#next) which calls the .__next__() method of an iterator.

You could add the underscores to the method in your code line = fo.__next__()- but it is probably best to use the built in function: line = next(fo)

I know this is an old question - but I felt it important to include this answer.

Erick Smith
  • 51
  • 1
  • 1
3

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

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

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

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+?

Community
  • 1
  • 1
RvdK
  • 19,580
  • 4
  • 64
  • 107
  • Whups, I checked it, it should be r+ http://stackoverflow.com/questions/10349781/how-to-open-read-write-or-create-a-file-with-truncation-possible – RvdK Nov 17 '14 at 07:49
  • 1
    This is half-correct - as far as I can tell `next()` is not a valid command on a text file object, hence the error he gets. – furkle Nov 17 '14 at 08:06
  • No, I just tried it. Open() returns an iterable object. https://docs.python.org/2/library/stdtypes.html#iterator.next – RvdK Nov 17 '14 at 08:26
  • Also if it was not the case, it would have not worked in forloops. – RvdK Nov 17 '14 at 08:27
  • That's the documentation for python 2.x. Both he and I are on 3.x. For me, I can iterate through the file object, but .next() results in the same error. – furkle Nov 17 '14 at 08:28
  • For 3.x, the documenation says it's https://docs.python.org/3/library/stdtypes.html#iterator.__next__. (So yes, you are right, cannot test it for 3.x here) – RvdK Nov 17 '14 at 09:42
1

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

gofr1
  • 15,741
  • 11
  • 42
  • 52
sinanorz
  • 11
  • 2