Python os.ttyname() Method
The Python os.ttyname() method is used to return a string that represents the terminal device associated with the file descriptor passed to it. In case, the file descriptor is not associated with a terminal device, this method will raise an exception.
Syntax
Following is the syntax for Python os.ttyname() method −
os.ttyname(fd)
Parameters
The Python os.ttyname() accepts only one parameter −
fd − This parameter specifies the file descriptor.
Return Value
The Python os.ttyname() method returns a string which specifies the terminal device.
Example
In the following example, we are checking the terminal device associated with the given file descriptor using os.ttyname() method.
import os, sys
# Showing current directory
print ("Current working dir :%s" %os.getcwd())
# Changing dir to /dev/tty
fd = os.open("/dev/tty",os.O_RDONLY)
p = os.ttyname(fd)
print ("the terminal device associated is: ")
print (p)
print ("done!!")
os.close(fd)
print ("file closed successfully!!")
When we run above program, it produces following result −
Current working dir :/home/tp/Python the terminal device associated is: /dev/tty done!! file closed successfully!!
python_files_io.htm
Advertisements