Python-os-ttyname

提供:Dev Guides
移動先:案内検索

Python os.ttyname()メソッド

説明

Pythonメソッド* ttyname()*は、_fd_に関連付けられた端末デバイスを指定する文字列を返します。 _fd_が端末デバイスに関連付けられていない場合、例外が発生します。

構文

以下は* ttyname()*メソッドの構文です-

os.ttyname(fd)

パラメーター

  • fd -これはファイル記述子です。

戻り値

このメソッドは、端末デバイスを指定する文字列を返します。

次の例は、ttyname()メソッドの使用法を示しています。

# !/usr/bin/python

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!!