0 votes
1 view
in Python by (1.6k points)

I'm trying to open a file and create a list with each line read from the file.

i=0

   List=[""]

   for Line in inFile:

      List[i]=Line.split(",")

      i+=1

   print List

But this sample code gives me an error because of the i+=1 saying that index is out of range. What's my problem here? How can I write the code in order to increment my list with every new Line in the InFile?

1 Answer

0 votes
by (25.1k points)

You can just use readlines() method from the file object, like this

with open("filename.txt") as f:

    lines = f.readlines()

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...