Python Open(), Read() & Readline() For Reading File
As we now have an idea of what files(text or binary) are and their access modes, we are now ready to dive into the discussion of file handling methods. When we want to read or write a file (say on our hard drive), we must first open the file. When we open a file, we are asking the operating system to find the file by name, making sure the file exists.
How to open a file?
Python has a built-in open() function to open a file.
The syntax of the function is:
To open a file, we must specify two things,
Name of the file and its extension
Access mode where we can specify in which mode file has to be opened, it could either be read (r), write (w) or append(a), etc.
The file “myfile.txt” will open in "rt" mode as it is the default mode. But the best practice is to follow the syntax to avoid errors.
The open function returns a file object. We store this file object into a variable which is generally called as a file pointer/file handler. Here is a code snippet to open the file using file handing in Python,
You can use this file pointer to further add modifications in the file. An error could also be raised if the operation fails while opening the file. It could be due to various reasons like trying to access a file that is already closed or trying to read a file open in write mode.
How to read a file?
To read a file in Python, there are various methods available,
We can read a whole file line by line using a for loop in combination with an iterator. This will be a fast and efficient way of reading data.
When opening a file for reading, Python needs to know exactly how the file should be opened. Two access modes are available reading (r), and reading in binary mode (rb). They have to be specified during opening a file with the built-in open() method.
The read() method reads the whole file by default. We can also use the read(size) method where you can specify how many characters we want to return i.e.
You can use the readline() method to read individual lines of a file. By calling readline() a second time, you will get the next line.
readlines() method reads until the end the file ends and returns a list of lines of the entire file. It does not read more than one line.
Note: The default mode to read data is text mode. If you want to read data in binary format, use ''rb".