What is the difference between readline and readlines function in python 3 | SoloLearn: Learn to code for FREE!

+1

What is the difference between readline and readlines function in python 3

5/31/2020 9:02:59 AM

Aman Yadav

2 Answers

New Answer

+9

Aman Yadav READLINES: readlines()- reads all the lines and put them in 'list'(between these [ ]) and displays them with each line ending with'\n' EXAMPLE:(for readlines) Say my text file contains the following: hello how are you COMMAND:(code to be entered) print(file.readlines()) OUTPUT:(for readlines) ['hello\n', 'how are\n', 'you \n'] READLINE: readline()- reads only the first line of the file. EXAMPLE:(for readline) (following the same example as above) COMMAND:(for readline) print(file.readline()) OUTPUT:(for readline) hello

+2

readline basically read single line from file & return as string where readlines reads all contents of the file & return data in lists of strings