English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
readline() This method is used to read an entire line from the file, including the "\n" character. If a non-negative number is specified, it returns the specified number of bytes, including the "\n" character.
readline() method syntax is as follows:
fileObject.readline(size)
size -- The number of bytes read from the file.
Return the bytes read from the string.
The following example demonstrates the use of the readline() method:
File w3The content of codebox.txt is as follows:
1:fr.oldtoolbag.com 2:fr.oldtoolbag.com 3:fr.oldtoolbag.com 4:fr.oldtoolbag.com 5:fr.oldtoolbag.com
Loop to read the content of the file:
# Open the file fo = open("w3codebox.txt, \ print("The file name is: ", fo.name) line = fo.readline() print("Read the first line %s" % (line)) line = fo.readline(5) print("The string read is: %s" % (line)) # Close the file fo.close()
The output result of the above example is:
The file name is: w3codebox.txt Read the first line 1:fr.oldtoolbag.com The string read is: 2:www