We can read files in Python with the help of its built-in functions. We deal with two types of files in Python.
- Text files: Each line of text ends with a special character(‘\n’) called End of Line character. The special character ‘\n’ is also called the Newline character in Python.
- Binary Files: Written in 0s and 1s. Here we don’t have any End of Line character. We store the data after converting it into a binary language which the machine understands.
Table of Contents
What is the access mode?
We pass access mode as a parameter to the open function, followed by the file name. The access mode is responsible for what kind of actions we are going to perform on the opened file. In case we try to do an operation that is not supported by that access mode, we will get an error.
The location of the filehandle in the file is also defined by the access mode. The filehandle acts like a cursor defining from where the data has to be read from the file or written into the file. The different access modes we use to read a file are:
- Read-only(‘r’): If we don’t specify the access mode, then the default mode is read-only. As the name specifies, we can only read the opened file, doesn’t allow any other operations. In read-only mode, the filehandle is situated at the beginning of the file. I/O error is raised when the file does not exist.
- Read and write(‘r+’): Unlike read-only mode, the read and write mode allows both reading and writing operations on the file. The filehandle is situated at the beginning of the file. I/O error is raised when the file does not exist
- Append and read(‘a+’): It allows both reading and writing operations on the file. If there is existing data in the file, then the new data will be written at the end. The filehandle is present at the end of the file. If the file doesn’t exist, it creates a new file with the name specified instead of raising the I/O error.
How to open a file:
We open a file using the open function. We can directly use the function, without the need for an import statement. As mentioned already we pass the filename and access mode as parameters to the open function.
Syntax:
file_oject=open(r'filename',access_mode)
If the file to read, is in the same directory as the python file, then we can use the file name directly. Else, we need to specify the full address of the file location.
Additionally, we use the character, r before the filename. The reason being, it prevents the characters in the filename to be mistaken as special characters. For instance, let’s say there is \temp in the address of the file. Python assumes that \t is a tab character and raises an error because of the invalid address. The r character tells that the filename is a raw string and does not contain any special characters. We can ignore it if the file is in the same directory as the Python file since the address of the file is not required.
Examples:
#opening a file present in the same directory file1=open('output.txt','r') #opening a file present in the different directory file2=open(r'C:\Users\UserName\Desktop\backup\myPython\Notebook\output1.txt','r+')
Errors:
- When we do an operation that is not supported by the access mode, we will get an error. Let’s try to write into a file, which is opened in read-only mode.
#opening a file present in the same directory file1=open('output.txt','r') file1.write('some text')
Traceback (most recent call last): File "sample.py", line 3, in <module> file1.write('some text') io.UnsupportedOperation: not writable
- If the file is not present in the location, we will get an error.
Traceback (most recent call last): File "sample.py", line 2, in <module> file1=open('output2.txt','r') FileNotFoundError: [Errno 2] No such file or directory: 'output2.txt'
How to close a file:
We use the close() function to close a file. We close a file under two scenarios:
- When we have no use for the file any longer.
- When we have to open the file in a different access mode.
The close() function is also responsible for freeing the memory space occupied by the file when closing the file.
Example:
file1=open('output.txt','r') file1.close()
How to read from a file:
Let’s get into the main part which is how to read from a file in python. There are three ways, in which we read from a file.
read():
Syntax:
file_object.read([n])
Read n bytes from a file. It reads the entire file if n is not specified. It returns the read bytes in a string format.
readline():
Syntax:
file_object.readline([n])
Reads the line of a file and returns it in a string format. It reads the maximum of n bytes in the line. It doesn’t read from more than one line, even if n is greater than the length of the line.
readlines():
Syntax:
file_object.readlines()
Read all the lines of a file and returns it as a list of all lines, each line in a string format. The new line character ‘\n’ is treated as a special character of two bytes.
Example:
#opening a file present in the same directory file=open('output.txt','w') data=["Book,Author\n","Book 1,Author X\n","Book 1,Author Y\n","Book 1,Author Z\n","Book 5,Author G\n","Book 27,Author F\n"] #writing data into file file.writelines(data) #closing file to read it in a read mode file.close() file=open('output.txt','r+') print("Output of read function:") print(file.read()) #seek takes the file to the nth byte from the beginner file.seek(0) print('Output of readline function:') print(file.readline()) file.seek(0) #Difference between read and readline function print('Output of read(20) function:') print(file.read(20)) print('\n') file.seek(0) print('Output of readline(20) function') print(file.readline(20)) file.seek(0) print('Output of readlines function') print(file.readlines())
Output of read function: Book,Author Book 1,Author X Book 1,Author Y Book 1,Author Z Book 5,Author G Book 27,Author F Output of readline function: Book,Author Output of read(20) function: Book,Author Book 1,A Output of readline(20) function Book,Author Output of readlines function ['Book,Author\n', 'Book 1,Author X\n', 'Book 1,Author Y\n', 'Book 1,Author Z\n', 'Book 5,Author G\n', 'Book 27,Author F\n']
You can see the difference between read and readline function, that in readline even if n exceeds the length of the line, its not reading from the next line.
With statement:
The with statement handles the exception. It also makes the code cleaner and readable. It simplifies the management of file resources by taking care of the acquisition and release of file resources. Hence we don’t need to close the file using close() function when we use with statement.
Syntax:
with open(r'filname',access_mode) as file
Example:
data=["Book,Author\n","Book 1,Author X\n","Book 1,Author Y\n","Book 1,Author Z\n","Book 5,Author G\n","Book 27,Author F\n"] #opening a file present in the same directory with open('output.txt','w') as file: file.writelines(data) with open('output.txt','r+') as file: print(file.read())
Here we are not closing the file, to open it in a different access mode. The with statement eliminates the need for the close() function.