Files in Python:
Python file handling is a way of saving program output to a file or reading data from a file. File handling is a crucial concept in the programming world. File management is used in almost every form of project. Assume you are constructing an inventory management system. You have data in the inventory management system related to sales and purchases, thus you must save that data somewhere. Using Python’s file management, you can save that data to a file. You must be given data in the form of a comma-separated file or a Microsoft Excel file if you wish to conduct data analysis. Using file handling, you can read data from a file and write output back into it.
File readline() Method in Python:
The readline() method reads one line from a file and returns it.
The size option can also be used to specify how many bytes from the line to return.
Syntax:
file.readline(size)
Parameters
size: This is Optional. The number of bytes to return from the line. The default value is -1, which implies the entire line.
Return Value:
This method’s return type is <class’str’>, which means it returns a string.
- Python Program to Delete Specific Line from a Text File
- Python Program to Copy Odd Lines of Text File to Another File
- Python Interview Questions on File Manipulation
File readline() Method with Examples in Python
Example1
Approach:
- Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
- Open the file in read mode. In this case, we’re simply reading the contents of the file.
- Print only one line(first) of the file using the readline() function.
- The Exit of Program.
Below is the implementation:
# Make a single variable to store the path of the file. This is a constant value. # This value must be replaced with the file path from your own system in the example below. givenFilename = "samplefile.txt" # Open the file in read mode. In this case, we're simply reading the contents of the file. gvn_file = open(givenFilename, 'r') # Print only one line(first) of the file using the readline() function. print(gvn_file.readline())
Output:
hello this is btechgeeks
Example2: To print first and second lines
Approach:
- Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
- Open the file in read mode. In this case, we’re simply reading the contents of the file.
- Print only one line(first) of the file using the readline() function.
- Again apply readline() method to the given file to get the second line of the file and print it. (you should call n number of times to read n lines)
- The Exit of Program.
Below is the implementation:
# Make a single variable to store the path of the file. This is a constant value. # This value must be replaced with the file path from your own system in the example below. givenFilename = "samplefile.txt" # Open the file in read mode. In this case, we're simply reading the contents of the file. gvn_file = open(givenFilename, 'r') # Print only the one line(first) of the file using the readline() function. print(gvn_file.readline()) # Again apply readline() method to the given file to get the second line of the file and print it. # (you should call n number of times to read n lines) print(gvn_file.readline())
Output:
hello this is btechgeeks good morning btechgeeks
Approach:
- Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
- Open the file in read mode. In this case, we’re simply reading the contents of the file.
- Pass some random number(size) to the readline() function to get the given number of bytes from the file and print the result.
- The Exit of Program.
Below is the implementation:
# Make a single variable to store the path of the file. This is a constant value. # This value must be replaced with the file path from your own system in the example below. givenFilename = "samplefile.txt" # Open the file in read mode. In this case, we're simply reading the contents of the file. gvn_file = open(givenFilename, 'r') # Pass some random number(size) to the readline() function to get the given number of bytes from the # file and print the result print(gvn_file.readline(10))
Output:
hello this
File Content:
hello this is btechgeeks good morning btechgeeks welcome to python-coding platform
Google Colab Images:
Files and Code: