Python codecs Library open and EncodedFile functions

Python codecs Library open and EncodedFile functions

Python codecs Library

The purpose of this module is Encoding and decoding i.e. conversion of the texts between different representations.
This module defines base classes for standard Python codecs (encoders and decoders) and provides access to the internal Python codec registry, which manages the codec and error handling lookup process.

The next two functions defined here are :
1., mode='r', encoding=None, errors='strict', buffering=-1):
2. codecs.EncodedFile(file, data_encoding, file_encoding=None, errors='strict'):, mode='r', encoding=None, errors='strict', buffering=- 1):

This function is used to open the encoded file using the given mode (read or write given as the second argument).
It returns an instance of StreamReaderWriter, providing transparent encoding or decoding.
Talking about the arguments the function takes:
The first argument is the name of the file which you want to open using this method.
The second argument is the mode with which you want to open the file. The default mode for mode is 'r' i.e. 'read' which means to open the file in the read mode. It must be noted that the underlying encoded files must be opened in the binary mode otherwise no automatic conversion of '\n' is done on reading and writing. Whatever be the mode of opening the file, it can be any binary mode acceptable to the built-in open() function; the 'b' is automatically added to it. Opening the file in the binary mode avoids the data loss that may occur when dealing with 8-bit encoding. 
The different modes that can be passed are:
'r': open file for reading
'w': open file for writing
'rb': open file in binary format for reading.
'wb': open file in binary format for writing.
The third argument is encoding. This specifies the encoding which is to be used for the file. Any encoding that encodes to and decodes from bytes is allowed and the data types supported by the file methods depend on the codec used.
The fourth argument 'errors' is passed and by default errors='strict' which causes a ValueError to be raised in case of encoding error occurs.
The last argument buffering is used to keep some buffer size for storing a chunk of file in a temporary memory until the file loads completely. It defaults to -1 which means that the default buffer size needs to be used.



codecs.EncodedFile(file, data_encoding, file_encoding=None, errors='strict'):

This function takes an open file handle using one encoding and wraps it with a class that translates the data to another encoding as the I/O occurs.
It returns a StreamRecoder instance, a wrapped version of the file which provides the transparent transcoding. The original file is closed when the wrapped version is closed. All the data written to the wrapped file id decoded according to the given 'data_encoding' and then written to the original file as bytes using file_encoding. Bytes read from the original file are decoded according to the file_encoding, and the result is encoded using data_encoding. If the file_encoding is not provided then the file encoding defaults to the data_encoding. The errors are to defining the error handling and it defaults to 'strict', which causes ValueError to be raised in case an encoding error occurs.


More Articles of Arkaja Sharan:

Name Views Likes
Python codecs Library Error Handling schemes module functions 52 0
Python codecs Library Error Handler register_error and lookup_error functions 51 0
Python codecs Library Error Handlers 50 0
Python codecs Library open and EncodedFile functions 48 0
Python codecs Library iterencode and iterdecode functions 53 0
Python codecs Library register and unregister functions 44 0
Python codecs Library getreader and getwriter functions 51 0
Python codecs Library getincrementalencoder and getincrementaldecoder 42 0
Python codecs Library getencoder and getdecoder functions 47 0
Python Introduction to codecs Library 68 0
Python fcntl Library flock and lockf functions 49 0
Python fcntl Library fcntl and ioctl functions 66 0
Python Resource Library resource usage functions 64 0
Python Resource Library resource usage symbolic constants 52 0
Python Resource Library Resource Limit Functions 61 0
Python resource library resource limit symbolic constants 58 0
Python Introduction to Resource Library 44 0
Python stringprep Library in_table_d1 and in_table_d2 functions 50 0
Python stringprep Library in_table_c8 and in_table_c9 functions 57 0
Python stringprep Library in_table_c5 in_table_c6 and in_table_c7 functions 47 0
Python stringprep Library in_table_c3 and in_table_c4 functions 50 0
Python stringprep library in_table_c21 in_table_c22 and in_table_c21_c22 44 0
Python stringprep library functions in_table_c11 in_table_c12 and in_table_c11_c12 53 0
Python Introduction to stringprep Library 53 0
Python unicodedata library is_normalized unidata_version and ucd_3_2_0 52 0
Python Unicodedata Library functions normalize and decomposition 101 0
Python Unicodedata Library functions east_asian_width and mirrored 53 1
Python Unicodedata Library category bidirectional and combining functions 87 0
Introduction to Unicodedata library lookup and name functions 51 0
Unicode Library decimal digit and numeric functions 55 0
Introduction to Unicode Data library 0 0