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?
filewill act as a lazygeneratorin first sample. The second one will read the store the file content in memory for read. - Anzel