What is the difference between read, redline and readlines ?
0
8082
Answer:
To read a file’s contents, call f.read(size), which reads some quantity of data and returns it as a string (in text mode) or bytes object (in binary mode). Size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned.
“somefile.txt” contains following lines
#This is first line
#This is second line
#This is third line
with open("somefile.txt", "r") as f:
data = f.read()
print(data)
#Out put
This is first line
This is second line
This is third line
f.readline() reads a single line from the file.
with open("some.txt", "r") as f:
data = f.readline()
print(data)
#Out put
This is first line
The readlines() method to get a list of string values from the file, one string for each line of text.
with open("somefile.txt", "r") as f:
data = f.readlines()
print(data)
#Output
['This is first line\n', 'This is second line\n', 'This is third line\n']
Comment here