Difference between readline and readlines? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Difference between readline and readlines?

3rd Dec 2016, 1:36 PM
MUTYALA SUBBARAYUDU
MUTYALA SUBBARAYUDU - avatar
3 Answers
+ 5
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
3rd Dec 2016, 1:45 PM
MUTYALA SUBBARAYUDU
MUTYALA SUBBARAYUDU - avatar
+ 1
readline(): print one line readlines(): print all lines
3rd Dec 2016, 1:46 PM
GuoKet
GuoKet - avatar
+ 1
but readlines()-it prints all in a single line with the following structure: ['Line 1 text \n', 'Line 2 text \n', 'Line 3 text']
3rd Dec 2016, 1:50 PM
MUTYALA SUBBARAYUDU
MUTYALA SUBBARAYUDU - avatar