Open() Built-in Function In Python

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:
    byte = fileObject.read()
    text = byte.decode(encoding = 'utf-8')
    print("Binary:")
    print(byte)
    print(type(byte))
    
    print("Text:")
    print(text)
    print(type(text))

Output:

Binary:
b'Hello World\n'
<class 'bytes'>
Text:
Hello World

<class 'str'>

Example 2 - Writing binary contents to a file:

# Example Python program that writes binary
# contents to a file

# File path
fileName    = "/ex/test.txt"   

# The "hello world" in hexadecimal format
txt            = b"\x68\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64"

# Open file in binary, write mode and write to it
with open(fileName, 'wb') as fileObject:
    fileObject.write(txt)

# Read back the contents and print
with open(fileName, 'rb') as fileObject:
    contents = fileObject.read()
    
    # Print the bytes read
    print(contents)
    print(type(contents))

    # Print the bytes as a string of hexa decimal values
    hexaString = contents.hex()
    print(hexaString)
    print(type(hexaString))

    # Print as string containing ascii characters
    asciiText = contents.decode("ascii")
    print(asciiText)
    print(type(asciiText))

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
# in text mode. The encoding used is utf-8.

# File path
fileName = "/pythonprogs/contents.txt"

# Open the file in text read mode and read the contents
with open(fileName, "rt", encoding = "utf-8") as textFile:
    line = textFile.readline()
    print(type(line))
    print(type(line.encode()))

    while line:
        # Print text
        print(line)
        
        # Print bytes
        print(line.encode())

        line = textFile.readline()

Output:

<class 'str'>
<class 'bytes'>
This is line one

b'This is line one\n'
and this is line two

b'and this is line two\n'

 

Example 4: Write text to a file

# Example Python program that writes text to a file
# through the file-object obtained through the built-in
# function open()

filePath = "/PythonProgs/Rhyme.txt"
mode     = "wt"
en          = "utf-8"

# Write text to a file
with open(filePath, mode, encoding = en) as textFile:
    textFile.write("Baby Shark...tutootututu...\n")
    textFile.write("Baby Shark!")

# Read text from a file
mode = "rt"
with open(filePath, mode, encoding = en) as textFile:
    
    ln = textFile.readline()

    while ln:
        print(ln)
        ln = textFile.readline()

 

Output:

Baby Shark...tutootututu...

Baby Shark!

 


Copyright 2023 © pythontic.com