Python os.ttyname()方法

Python的ttyname()方法返回一个字符串,它指定与fd关联的终端设备。 如果fd与终端设备没有关联,则会出现异常。

语法

以下是ttyname()方法的语法 -

os.ttyname(fd)

参数

  • fd - 这是文件描述符。

返回值

  • 该方法返回一个指定终端设备的字符串,仅在Unix系统上可用。

示例

以下示例显示了ttyname()方法的用法。

# !/usr/bin/python33
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 ("Closed the file successfully!!")

当运行上述程序时,得到如下所示: -

Current working dir is :/tmp
the terminal device associated is:
/dev/tty
done!!
Closed the file successfully!!

上一篇: Python os模块方法 下一篇: Python异常处理