Python os.ttyname () method
Python OS file / directory methods
Outline
os.ttyname () method returns a string that represents the file descriptor fd is associated with a terminal device. If fd is not associated with a terminal device, an exception is thrown.
grammar
ttyname () method syntax is as follows:
os.ttyname(fd)
parameter
fd - file descriptor
return value
It returns a string that represents the file descriptor fd is associated with a terminal device.
Examples
The following example demonstrates the use of 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 above program output is:
当前目录 :/tmp 关联的终端为: /dev/tty done!! 关闭文件成功!!