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.

Code: Use of os.device_encoding() method to get the encoding of the device associated with the given file descriptor

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)) 

chevron_right


Output:

Connected to a terminal: False
Device encoding: None
Connected to a terminal: True
Device encoding: UTF-8


My Personal Notes arrow_drop_up

self motivated and passionate programmer

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



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.