What is difference between read() and readlines() in python

So I didn’t get the use cases of read() and readlines() if someone could explain it to me in simple terms as I am new to programming it would be great.

@ajax9666982763 Welcome to the forums. This may not be too helpful, but I will try my best:
read() function reads an entire file, as though it were a string. This means that it treats each character differently, so if you were trying to iterate over a file e.g:

file_1 = open('file', 'r')
for i in file_1.read():
pass #or code

The .read() would treat each character in the file separately, meaning that the iteration would happen for every character.

The readline() function, on the other hand, only reads a single line of the file. This means that if the file file_1 were three lines long, the readline() function would only parse (or iterate/operate) on the first line of the file.
I hope this helps!
Happy coding!

2 Likes

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.