How to Read from a File in Python?
Before we read files using Python, we should first define files. We use a contiguous set of bytes called a file to store any data. A file could be as simple as a text or an executable file. These file bytes are always converted to binary ( 0 and 1) format for processing by the computer.
Here are some types of files that Python can handle
- Text files
- Binary files
Python can handle other files, including CSV files, excel sheets, etc. But this article will focus on the text and binary files.
What is the difference between the two files? In the text file, each line is terminated using a newline character \n. In contrast, a binary file does not have any such terminator, and the file's data can be stored after converting the binary file into binary language that the machine can understand.
We can now move on to reading files with the help of Python.
File Access Modes
When you open a file, you want to perform an operation on it. So, depending on the type of operations you’d like to perform, you select an access mode to open the file.
Typically, there are six access modes:
Read-only (‘r’): Denoted by r. This serves as the default mode for file opening. If the file you wish to access does not exist, it raises an I/O error. With this access mode, you can only read a particular file.
Write only (‘w’): This is the access mode you use to write in a file you’d like to open. Note that in this mode, if your file already exists, then whatever you wish to write into your file will get written but will delete all the initial contents of the file. When a file does not exist, a new file will be created.
Append (‘a’): The append access mode allows you to write to opened files. Then what’s the difference between the append and write mode? Well, in the append mode, the initial contents of the file are not deleted. Whatever you wish to write in the file will be written after the initial contents of the file.
Read and Write (‘r+’): This option enables both reading from and writing to a file, with the handle starting at the file's beginning. An error is triggered if the file doesn't exist.
Write and Read (‘w+’): You might be wondering the difference between read and write mode and this mode. Since this mode is an extension of the write mode, whatever you write into the file will be written after the truncation of existing contents.
Append and Read (‘a+’): As the name suggests, you can read and append to a file in this access mode. Any data you wish to add will be added after the existing data.
Opening a File
To open files in Python, the built-in open() function is used with specified access modes, such as 'w', 'w+', 'a', or 'a+'. The access modes determine whether the file is opened for writing, reading, or both, and whether the file is created if it doesn't exist. The syntax is:
The 'r' before the file parameters is used to treat the string as raw, preventing special characters in the file name from causing errors. The open function provides a file object for executing operations.
It's essential to close files after opening them. Additionally, specifying the encoding is recommended when dealing with text files, like:
Reading from a File
Now that we know the modes in which we can access a file and how to open a file let’s get down to reading from it.
Naturally, if you need to read from a file in Python, you would use the r mode.
To read data from a text file in Python, you have three possible methods:
1. read() Method
The read method returns a string containing all the file characters mentioned. This function has no mandatory parameters, but if you want to read a certain number of characters from the file, you can specify them as a parameter.
Now, say the file example.txt has the following contents:
To read the first five characters:
What if you wanted to read five characters but not from the current position? You want to skip the first 12 characters and print the following 5. How would you do that?
Python has a function that gives you the cursor’s current position while handling a file. You can also ‘seek’ a certain position in the file.
The read() function only allows you to either read the file's entire contents or a certain number of specified characters. What if you wanted to read the first line or the third?
2. readline() Method
The readline() function helps you read a single line from the file. You can run this function as often as you require to get the information. Every time you run the function, it returns a string of the characters that it reads from a single line from the file.
If you specify an n in the parameters, it reads the n number of characters from that line in the file. If n exceeds the number of characters in the file, the method returns the line without reading the following line.
Let’s assume the contents of the file are:
Code to open:
3. readlines() Method
As the name suggests, the readlines() method reads all the lines in the file and returns them properly separated in a list.
Most of the time, you will not use functions like this. You might want to loop over the lines for some data you want to extract, etc. A faster and more memory-efficient way to do so is given below.
The contents of our new file are now:
The code to access the above contents of a file is:
Reading from a Binary File
At the beginning of this article, we discussed two file types. In all the above examples, we’ve seen how to read a text file in Python. All the access modes, too, were only for text files. What about binary files?
We have special access modes to read/write to binary files: rb and wb. Of course, rb is for reading and wb for writing.
It is important to note here that the return type of any operations on binary files with these access modes will be in the form of byte strings and not text strings like in text files. Similarly, when you’re writing to files, the format must also be binary.
Closing a File
The built-in close() function is used to close a file in Python. This function releases the memory space acquired by the file. If you no longer need the file or require it in another access mode, you can use close().
To ensure file closure, even in the presence of exceptions during file handling, a recommended approach is using the try … finally block. Code within the try block is executed initially, and in the event of an error, it proceeds to the final block to ensure proper closure of the file.
After learning about Reading Files in Python, here are some key points that will help you:
- In Python, we can handle files such as :
- Text files
- Binary files
- CSV files
- Excel Sheets etc.
- In Python, there are six access modes for file handling :
- Read only (‘r’)
- Write only (‘w’)
- Append (‘a’)
- Read and Write (‘r+’)
- Write and Read (‘w+’)
- Append and Read (‘a+’)
- We have three methods for reading text files in Python :
- read() method
- readline() method
- readlines() method