Python3 Os Ttyname
# Python3.x Python3 os.ttyname() Method
[ Python3 OS File/Directory Methods](#)
* * *
### Overview
The `os.ttyname()` method is used to return a string representing the terminal device associated with the file descriptor `fd`. If `fd` is not associated with a terminal device, an exception is raised.
### Syntax
The syntax for the `ttyname()` method is as follows:
os.ttyname(fd)
### Parameters
* **fd** -- file descriptor
### Return Value
Returns a string representing the terminal device associated with the file descriptor `fd`.
### Example
The following example demonstrates the use of the `ttyname()` method:
#!/usr/bin/python3import os, sys # Display the current directoryprint ("Current directory :%s" %os.getcwd())# Change directory to /dev/tty fd = os.open("/dev/tty",os.O_RDONLY) p = os.ttyname(fd)print ("Associated terminal is: ")print (p)print ("done!!") os.close(fd)print ("File closed successfully!!")
The output of executing the above program is:
Current directory :/tmp Associated terminal is:/dev/tty done!!File closed successfully!!
[ Python3 OS File/Directory Methods](#)
YouTip