Overview:
-
Based on the type of I/O selected, the function fdopen() instantiates one of the IOBase derived classes and returns the object. For example, when text I/O is selected the function returns an instance of TextIOWrapper. If binary I/O is selected the function returns an instance of FileIO.
-
The behaviour is the same as that of the built-in function open(). While the built-in function open() creates and returns the the required file object using a file path, the function fdopen() constructs the file object using a file descriptor.
-
A file descriptor can be created using functions like os.open().
Example:
# Example Python program that creates a # Open a file and get the file descriptor # Create a file object from the file descriptor # Append text # Read the contents back and print # Read and print the file contents |
Output:
Rain, Rain, Go away; Come again, Another day; |