import os from typing import IO with open('file.txt') as f: # Opening file in read mode fd: int = f.fileno() # Obtaining file descriptor os.write(fd, b'Hello world!') # Writing bytes to file
import os from typing import IO fd = os.open('file.txt', os.O_RDONLY) # Opening file and obtaining file descriptor f: IO[str] = open(fd, mode='r', closefd=False) # Creating file object using file descriptor print(f.read()) # Reading contents of file f.close() # Closing file objectIn this example, we open a file named 'file.txt' using the os.open() method and obtain its file descriptor. We then use the open() method with this file descriptor to create a file object and read its contents using the read() method. Finally, we close the file object using the close() method. Package Library: Built-in module "typing", Built-in module "os".