The readline() method

In order to read the file line by line, use readline().

Let's see the sample code readline1.py:

file_input = open("sample1.txt",'r')
print file_input.readline()
print file_input.readline()
print file_input.readline()
file_input.close()

Let's see the output of code:

In code, we have printed three lines. What happens, if you specify count in readline(count). See the code readlinecount.py:

file_input = open("sample1.txt",'r')
print file_input.readline(100)
print file_input.readline(20)
file_input.close()

 Let's see the output:

In the file_input.readline(100) syntax prints 100 characters of the first line. But the first line contains only 48 characters. The syntax file_input.readline(20) prints the 20 characters from the second line of sample1.txt.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.8.8