无涯教程-OS File - os.ttyname(fd)函数

72 阅读1分钟

Python方法ttyname()返回一个字符串,该字符串指定与fd关联的终端设备。如果fd不与终端设备关联,则引发异常。

os.ttyname(fd) - 语法

os.ttyname(fd)
  • fd  -  这是文件描述符。

os.ttyname(fd) - 返回值

此方法返回指定终端设备的字符串。

os.ttyname(fd) - 示例

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

# !/usr/bin/python

import os, sys

# 显示当前目录 print "Current working dir :%s" %os.getcwd()

# 将目录更改为 /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!!

参考链接

www.learnfk.com/python/os-t…