Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating over lines in a file python

Tags:

I am learning python. Would it be possible if someone could explain the different between the following for processing a file:

file = open("file.txt") for line in file:     #do something  file = open("file.txt") contents = file.read() for line in contents:     # do something 

I know that in the first case, the file will act as a list so we iterate over a file as we iterate over the elements of a list but in the second case, I am not sure how to explain what happens if I read the file first and then iterate over it?

like image 634
Homap Avatar asked Jul 28 '15 08:07

Homap


People also ask

How do you iterate through a file line by line in Python?

To read a file word by word in Python, you can loop over each line in a file and then get the words in each line by using the Python string split() function.

How do I iterate a text file in Python?

Just iterate over each line in the file. Python automatically checks for the End of file and closes the file for you (using the with syntax). Show activity on this post. This will work because the the readline() leaves a trailing newline character, where as EOF is just an empty string.

Which function will be used to iterate over file object 1 line at a time in a for loop?

for line in myFile: statement1 statement2 ... To process all of our climate change data, we will use a for loop to iterate over the lines of the file. Using the split method, we can break each line into a list containing all the fields of interest about climate change.


1 Answers

In the first one you are iterating over the file, line by line. In this scenario, the entire file data is not read into the memory at once; instead, only the current line is read into memory. This is useful for handling very large files, and good for robustness if you don't know if the file is going to be large or not.

In the second one, file.read() returns the complete file data as a string. When you are iterating over it, you are actually iterating over the file's data character by character. This reads the complete file data into memory.

Here's an example to show this behavior.

a.txt file contains

Hello Bye 

Code:

>>> f = open('a.txt','r') >>> for l in f: ...     print(l) ... Hello  Bye   >>> f = open('a.txt','r') >>> r = f.read() >>> print(repr(r)) 'Hello\nBye' >>> for c in r: ...     print(c) ... H e l l o   B y e 
like image 131
Anand S Kumar Avatar answered Sep 27 '22 21:09

Anand S Kumar