+ 2
Difference between readline and readlines?
3 ответов
+ 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
+ 1
readline(): print one line
readlines(): print all lines
+ 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']