3. INTRODUCTION
Python is a popular programming language that offers a
range of file handling functions for developers.
One such function is the open() function, which is used to
open and manipulate files.
The open() function comes with eight different parameters
that allow developers to customize their file handling
operations according to their specific requirements.
In this presentation we are going to deal with the eight
parameters of "open()" function of Python's file handling with
suitable example codes.
4. PARAMETER 1: FILE
The file parameter is the first parameter of the open() function and specifies the name of the file that needs
to be opened.
This can include the file path as well. If the file does not exist, the open() function creates a new file with the
specified name.
SYNTAX: file = open(filename, mode)
Filename:iThis parameter as the name suggests, is the name of the file that we want to open.
le that we want to open.
Mode: This parameter is a string that is used to specify the mode in which the file is to be opened. The
following strings can be used to activate a specific mode: “r” , “a” , “w” , “x” , “b” , “t”.
Example code: file = open('example.txt', 'r’)
print(file.read())
This code opens a file named 'example.txt' in read mode and prints its contents.
5. PARAMETER 2: MODE
The mode parameter is the second parameter of the open() function and
specifies the mode in which the file needs to be opened.
The most commonly used modes are read (r), write (w), and append (a).
Example code: file = open('example.txt', 'w’)
file.write('This is an example text file.’)
file.close()
This code opens a file named 'example.txt' in write mode, writes the
specified text to it, and then closes the file.
6. PARAMETER 3: BUFFERING
The buffering parameter is the third parameter of the open() function and
specifies the buffer size for reading and writing operations.
The default value is -1, which means that the system default buffer size is
used.
Example code: file = open('example.txt', 'r', buffering=1024)
print(file.read())
This code opens a file named 'example.txt' in read mode with a buffer size
of 1024 bytes and prints its contents.
7. PARAMETER 4: ENCODING
The encoding parameter is the fourth parameter of the open() function
and specifies the character encoding used to read or write the file.
The default value is None, which means that the system default encoding
is used.
Example code: file = open('example.txt', 'r', encoding='utf-8’)
print(file.read())
This code opens a file named 'example.txt' in read mode with UTF-8
encoding and prints its contents.
8. PARAMETER 5-8 : ERROS, CLOSEFD,OPENER,NEWLINE
The remaining four parameters of the open() function are errors, newline, closefd, and opener.
The errors parameter specifies how errors should be handled while reading or writing files.
The newline parameter specifies the type of newline character used in the file.
The closefd parameter specifies whether the file descriptor should be closed after the file is opened.
The opener parameter specifies a custom opener function that can be used to open the file.
Example code: file = open('example.txt', 'r’,
errors='ignore', newline=‘’,
closefd=False)
print(file.read())
This code opens a file named 'example.txt' in read mode with errors ignored, no newline character, and
without closing the file descriptor.