임종훈
임종훈

Reputation: 187

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

everybody. I am currently working to merge the csv files. For example, you have files from filename1 to filename100. I used the following code to combine 100 files and the following error occurred: I'll put the code up first. import csv

fout=open("aossut.csv","a")
# first file:
for line in open("filename1.csv"):
    fout.write(line)
# now the rest:    
for num in range(2,101):
    f = open("filename"+str(num)+".csv")
    f.next() # skip the header
    for line in f:
         fout.write(line)
    f.close() # not really needed
fout.close()

And the following error occurred when the above file was executed:

File "C:/Users/Jangsu/AppData/Local/Programs/Python/Python36-32/tal.py", line 10, in 
<module>
    f.next() # skip the header
AttributeError: '_io.TextIOWrapper' object has no attribute 'next'

I've been working on it for a few days, and I don't know what to do.

Upvotes: 2

Views: 3328

Answers (2)

amirhosein majidi
amirhosein majidi

Reputation: 149

The method "next()" in csv library is updated to next() in python 3. you can see the details in this link : https://docs.python.org/3/library/csv.html

Upvotes: 0

Sunitha
Sunitha

Reputation: 12015

The file object doesn't have next method. Instead use next(f) to skip the first line

for num in range(2,101):
    with open("filename"+str(num)+".csv") as f:
        next(f)
        for line in f:
            fout.write(line)

Upvotes: 5

Related Questions