- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python float() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python ascii() Function
- Python pow() Function
- Python type() Function
- Python List Functions
- Python list() Function
- Python insert() Function
- Python append() Function
- Python extend() Function
- Python pop() Function
- Python remove() Function
- Python reverse() Function
- Python sort() Function
- Python sorted() Function
- Python Dictionary Functions
- Python dict() Function
- Python update() Function
- Python get() Function
- Python keys() Function
- Python setdefault() Function
- Python fromkeys() Function
- Python items() Function
- Python popitem() Function
- Python Tuple Function
- Python tuple() Function
- Python Set Functions
- Python set() Function
- Python frozenset() Function
- Python String Functions
- Python split() Function
- Python join() Function
- Python format() Function
- Python replace() Function
- Python Iterator Functions
- Python iter() Function
- Python min() Function
- Python max() Function
- Python sum() Function
- Python count() Function
- Python index() Function
- Python copy() Function
- Python clear() Function
- Python next() Function
- Python filter() Function
- Python enumerate() Function
- Python zip() Function
- Python reversed() Function
- Python Number Functions
- Python abs() Function
- Python bin() Function
- Python oct() Function
- Python hex() Function
- Python round() Function
- Python divmod() Function
- Python complex() Function
- Python File Handling Functions
- Python open() Function
- Python read() Function
- Python readable() Function
- Python readline() Function
- Python readlines() Function
- Python write() Function
- Python writable() Function
- Python writelines() Function
- Python close() Function
- Python seek() Function
- Python tell() Function
- Python flush() Function
- Python fileno() Function
- Python truncate() Function
- Python Class Functions
- Python object() Function
- Python property() Function
- Python getattr() Function
- Python setattr() Function
- Python hasattr() Function
- Python delattr() Function
- Python classmethod() Function
- Python staticmethod() Function
- Python issubclass() Function
- Python super() Function
- Python Misc Functions
- Python all() Function
- Python any() Function
- Python isatty() Function
- Python bool() Function
- Python callable() Function
- Python globals() Function
- Python locals() Function
- Python dir() Function
- Python id() Function
- Python isinstance() Function
- Python map() Function
- Python repr() Function
- Python slice() Function
- Python vars() Function
- Python Advance Functions
- Python help() Function
- Python hash() Function
- Python breakpoint() Function
- Python bytes() Function
- Python bytearray() Function
- Python memoryview() Function
- Python compile() Function
- Python eval() Function
- Python exec() Function
- Python Tutorial
- Python Tutorial
- Python Examples
- Python Examples
Python readline() Function
Unlike readlines(), the readline() function in Python, used to read first line from the file.
If there are multiple statements of readline() function available in same program. Then first readline() returns first line, second readline() returns second line and so on.
Python readline() Syntax
The syntax to use readline() function in Python is:
fo.readline(size)
where fo refers to the file object or handler. The size parameter is optional. If the size parameter of readline() provided as 10, then only first 10 characters of the line gets returned or read. Otherwise if this parameter is empty, then the whole line gets returned.
Python readline() Example
Before creating the program that uses readline() function to read the line from a file. We need to create a file first. Therefore I'm going to create a file say file.txt with some lines say 3 lines. The file must be saved within the current directory. Here is the snapshot of the current directory, with the file file.txt:
Now it's time to create an example program of readline() function:
fo = open("file.txt", "r") myline = fo.readline() print(myline)
The snapshot given below shows the sample output produced by this Python program:
See, only first line returned by the function readline(), that was printed on the output using print() statement. Now let's use the size parameter of this function as shown in the program given below:
fo = open("file.txt", "r") myline = fo.readline(7) print(myline)
This time, the output will be:
This is
See, only first 7 characters including space is returned by the function readline() with 7 as parameter value.
Now the question is, what if file does not exist ?
Therefore, let's modify the above program, that handles error raised when the file given does not available in the current
directory. Also the modified program, receives the name of file from user at run-time of the program. And I've used multiple
readline() functions in single program shown below:
print("Enter File's Name: ", end="") filename = input() try: file_object = open(filename, "r") print("\n----Content of File----") print(file_object.readline()) print(file_object.readline()) except FileNotFoundError: print("\nThe given file is not available.")
Here is its sample run with user input file.txt:
The double newline printed, one due to the newline available as the last character of the line returned by readline(), and other due to the default behavior of print(). To avoid double newline, replace the following statement from above program:
print(file_object.readline())
with the statement given below:
print(file_object.readline(), end="")
Note: The end= parameter is used to change the default behavior of print(). To learn in detail, refer to its separate tutorial.
« Previous Function Next Function »