OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.
os.device_encoding()
method in Python is used to get the encoding of the device associated with the specified file descriptor, if it is connected to a terminal. This method returns None, if the specified file descriptor is not connected to a terminal.
Note: This method is available only on some flavour of UNIX.
Syntax: os.device_encoding(fd)
Parameter:
fd: A file descriptor, whose device encoding is to be queried.Return Type: This method returns a string value which represents the encoding of the device associated with the specified file descriptor if it is connected to a terminal, otherwise None.
# Python program to explain os.device_encoding() method # importing os module import os # File path path = "/home/ihritik/Desktop/file.txt" # Open the file and get # the file descriptor associated # with it using os.open() method fd = os. open (path, os.O_RDWR | os.O_CREAT) # Check if file descriptor fd # is open and connected # to a terminal using os.isatty() method print ( "Connected to a terminal:" , os.isatty(fd)) # Print the encoding of # the device associated with # the file descriptor fd # using os.device_encoding() method print ( "Device encoding:" , os.device_encoding(fd)) # Open a new pseudo-terminal pair # using os.openpty() method # It will return master and slave # file descriptor for # pty ( pseudo terminal device) and # tty ( native terminal device) respectively master, slave = os.openpty() # Check if file descriptor master # is open and connected # to a terminal using os.isatty() method print ( "Connected to a terminal:" , os.isatty(master)) # Print the encoding of # the device associated with # the file descriptor master # using os.device_encoding() method print ( "Device encoding:" , os.device_encoding(master)) |
Connected to a terminal: False Device encoding: None Connected to a terminal: True Device encoding: UTF-8