File handling is one of the important feature in programming languages. Python provides a way to handle(read, write & modify) both text files as well as binary files. This tutorial will cover open() function in detail.
➠ There are various functions available in Python for File handling. Click the function to get more detail about that function.fileVariableName = open( filename_with_full_path, open_mode) . . fileVariableName.close()
with open(filename_with_full_path, open_mode) as fileVariableName: . .
Mode |
Description / Functionality |
r |
This is default and opens a file in reading mode. It will result in error if the file does not exist. |
a |
Opens a file in append mode. It will create file if the file does not exist. |
w |
Opens a file in writing mode. It will create file if the file does not exist. |
x |
Opens a file in writing mode(same as 'w'). It will result in error if the file exists. |
+ |
Opens a file in read/write or write/read or append/read mode depending on the other mode specified. This must be used along with append(a), read(r) or write(w) mode. |
Mode |
Description / Functionality |
t |
This is default and it is meant to open a file in text mode for text related operations. |
b |
It meant to open a file in binary mode for binary related operations(e.g. working with images). |
fileVar = open("/complete_path_to_file/pythonTest.txt", "r")
print(fileVar.read())
fileVar.close()
Output:
Line 1: This is Python File Handling
Line 2: This is Python File Handling Testing File
with open("/complete_path_to_file/pythonTest.txt", "r") as fileVar:
print(fileVar.read())
Output:
Line 1: This is Python File Handling
Line 2: This is Python File Handling Testing File
fileVar = open("/complete_path_to_file/pythonTestWrite.txt", "w")
fileVar.write('This is Python File Writing\n')
fileVar.close()
Output:
*******pythonTestWrite.txt content********
This is Python File Writing <newline Character>
******************************************
fileVar = open("/complete_path_to_file/pythonTest.txt", "x")
Output:
FileExistsError: [Errno 17] File exists: '/complete_path_to_file/pythonTest.txt'
fileVar = open("/complete_path_to_file/pythonTestWrite.txt", "a")
fileVar.write('\nThis is another line')
fileVar.close()
Output:
*******pythonTestWrite.txt content********
This is Python File Writing
This is another line
******************************************
fileVar = open("/complete_path_to_file/pythonTestWrite.txt", "rt")
print(fileVar.readline())
fileVar.close()
Output:
This is Python File Writing <newline Character>
fileVar = open("/Users/Pradeep/Desktop/websites/dbmstutorials/favicon.ico", "rb")
print(fileVar.read(10))
fileVar.close()
Output:
b'\x00\x00\x01\x00\x01\x00 \x00\x00'
==> If users will try to read binary file in text format then Python will throw below error.
Traceback (most recent call last):
File "", line 1, in
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa8 in position 14: invalid start byte
fileVar = open("/complete_path_to_file/pythonTest.txt", "rb")
print(fileVar.readline())
fileVar.close()
Output:
b'Line 1: This is Python File Handling\n'
fileVar = open("/complete_path_to_file/pythonTest.txt", "rb")
print(fileVar.readline().decode('utf-8'))
Output:
Line 1: This is Python File Handling <newline Character>
fileVar = open("/complete_path_to_file/pythonTestWrite.txt", "r+")
fileVar.write('This will overwrite characters')
print(fileVar.read(15))
fileVar.close()
*****pythonTestWrite.txt content Before********
This is Python File Writing
This is another line
***********************************************
Output:
This will overw
*******pythonTestWrite.txt content After********
This will overwrite characters
s is another line
************************************************
fileVar = open("/complete_path_to_file/pythonTestWrite.txt", "w+")
fileVar.write('This will overwrite characters')
fileVar.close()
*****pythonTestWrite.txt content Before********
This is Python File Writing
This is another line
***********************************************
Output:
*******pythonTestWrite.txt content After********
This will overwrite characters
************************************************
fileVar = open("/complete_path_to_file/pythonTestWrite.txt", "a+")
fileVar.write('\nThis will append at the end of file')
fileVar.close()
*****pythonTestWrite.txt content Before********
This is Python File Writing
This is another line
***********************************************
Output:
*******pythonTestWrite.txt content After********
This is Python File Writing
This is another line
This will append at the end of file
************************************************