Overview:
-
The open() built-in function returns a file object that supports reading and writing to files in binary and text formats. I/O in text format supports several built-in encodings and other encodings based on the availability of codecs.
-
The open() function is versatile in handling most of the file I/O requirements. Based on the type of I/O needed – whether binary I/O or text I/O, open() function takes care of creating the appropriate instance from a variety of I/O classes and returning it for read/write/update operations on files.
-
While the os.open() function provides the lowest level of API that a developer can use for creating a file descriptor and perform low level I/O operations using the functions provided by the operating system API(i.e., functions from the os module) the built-in function open() abstracts such low level details.
-
Using open() not only abstracts dealing with the low level os module functions, to some extent it also abstracts dealing with specialized I/O related classes.
- The open() built-in function takes care of the most common requirements of file I/O like buffering, encoding, decoding and handling of errors during encoding and decoding, creation of custom opener in case a special file descriptor is needed.
Buffering:
-
Buffering in a nutshell is reading optimal number of bytes, often in multiples of the file system block size instead of going to disk for each byte of read/write/update operations.
-
In binary mode, when buffering is used either by selecting the default buffering strategy or by specifying a buffer size that suits the need of the problem in hand or the underlying platform, the built-in function open() will internally create a BufferedReader or a BufferedWriter or a BufferedRandom instance and it to read/write the bytes. The binary mode will use the default block size or a custom block size.
-
When no buffering is specified in binary mode, open() returns an instance of FileIO which is derived from the class RawIOBase. However, there are optimisations through buffering when readall() is invoked on the FileIO instance in binary mode, even when the buffering is switched off.
-
In case of text I/O an instance of TextIOWrapper is returned for reading and writing text lines.
Example 1 - Reading binary data from a file:
The file object obtained through binary mode using open() can be issued one or more read() calls based on the size of the available data. Each read() call can read a single byte or a chunk of bytes from the file. The bytes read are raw bytes. If they need to be converted to a string it can be done through the decode method of the bytes class by specifying how to decode the bytes (i.e.,using the encoding parameter).
fileName = "/ex/test.txt" with open(fileName, 'rb') as fileObject: |
Output:
Binary: <class 'str'> |
Example 2 - Writing binary contents to a file:
# Example Python program that writes binary # File path # The "hello world" in hexadecimal format # Open file in binary, write mode and write to it # Read back the contents and print # Print the bytes as a string of hexa decimal values # Print as string containing ascii characters |
Output:
b'hello world' <class 'bytes'> 68656c6c6f20776f726c64 <class 'str'> hello world <class 'str'> |
Example 3 - Read text contents from a file
# Example Python program that reads contents from a file # File path # Open the file in text read mode and read the contents while line: line = textFile.readline() |
Output:
<class 'str'> b'This is line one\n' b'and this is line two\n' |
Example 4: Write text to a file
# Example Python program that writes text to a file filePath = "/PythonProgs/Rhyme.txt" # Write text to a file # Read text from a file while ln:
|
Output:
Baby Shark...tutootututu... Baby Shark! |