Python | os.device_encoding() method
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
Recommended Posts:
- class method vs static method in Python
- Python | set() method
- Python | next() method
- Python | os.dup() method
- Python PIL | ImageChops.add() method
- Python | os.pread() method
- Python | os.fsencode() method
- Python | Tensorflow cos() method
- Python | os.system() method
- Python PIL | copy() method
- Python | os.isatty() method
- Python | os.getrandom() method
- Python | os.makedev() method
- Python | sympy.has() method
- Python | sympy.Mod() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai




