Python os.ttyname() Method
Python 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
ttyname()The method syntax is as follows:
os.ttyname(fd)
Parameters
fdfd -- 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/python
# -*- coding: UTF-8 -*-
import os, sys
# 显示当前目录
print "当前目录 :%s" %os.getcwd()
# 修改目录为 /dev/tty
fd = os.open("/dev/tty",os.O_RDONLY)
p = os.ttyname(fd)
print "关联的终端为: "
print p
print "done!!"
os.close(fd)
print "关闭文件成功!!"
The output of the above program is:
当前目录 :/tmp 关联的终端为: /dev/tty done!! 关闭文件成功!!Other Extensions