In this tutorial, we will learn how to take input from a file in Python. Reading files is an essential skill for any programmer and is especially useful when dealing with data analysis, configuration files, or input data for your program. Python provides easy-to-use functions that allow you to read data from a file with minimal effort.
Step 1: Create a sample input file
Before we start reading data from a file, let’s first create a sample input file. In this example, we will use a simple text file containing the following data:
Apple Banana Cherry Date
Save this data in a text file named fruits.txt in your working directory.
Step 2: Open the file in Python
In order to read data from a file, you need to first open the file in Python. You can use the built-in open() function to open the file. The open function takes two arguments: the name of the file and the access mode, which determines whether you want to read or write data to the file.
In this tutorial, we will use the ‘r’ mode to open the file, which means the file is opened for reading.
1 |
file = open("fruits.txt", "r") |
Step 3: Read the data from the file
There are several methods to read data from a file. Let’s discuss some common methods:
- read(): This method reads the entire contents of the file and returns a string.
1 2 |
data = file.read() print(data) |
- readline(): This method reads a single line from the file.
1 2 |
line = file.readline() print(line) |
- readlines(): This method returns a list containing all the lines of the file.
1 2 |
lines = file.readlines() print(lines) |
Step 4: Close the file
After you are done reading data from the file, it is a good practice to close the file using the close() method. This helps in freeing up system resources and prevents possible file corruption.
1 |
file.close() |
For the above-mentioned methods, here is the complete code to read the data from the file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# Open the file file = open("fruits.txt", "r") # Read the entire contents of the file data = file.read() print("Data using read():\n", data) # Reset the file pointer to the start of the file file.seek(0) # Read a single line from the file line = file.readline() print("Data using readline():\n", line) # Reset the file pointer to the start of the file file.seek(0) # Read all the lines from the file lines = file.readlines() print("Data using readlines():\n", lines) # Close the file file.close() |
Output:
1 2 3 4 5 6 7 8 9 10 |
Data using read(): Apple Banana Cherry Date Data using readline(): Apple Data using readlines(): ['Apple\n', 'Banana\n', 'Cherry\n', 'Date\n'] |
Step 5: Use a context manager to handle files
A more efficient way to handle files in Python is using a context manager, which automatically takes care of opening and closing the file for you. The with
keyword is used to create a context manager. Below is an example of how to read data from a file using a context manager:
1 2 3 |
with open("fruits.txt", "r") as file: data = file.read() print(data) |
In this case, you don’t have to explicitly call the close()
method, as the context manager takes care of it.
Conclusion
In this tutorial, you have learned how to read data from a file in Python using various methods such as read()
, readline()
, and readlines()
. The context manager is the more efficient way to handle files in Python, as it takes care of opening and closing the file automatically.