Python3-os-ttyname

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

Python 3-os.ttyname()メソッド

説明

メソッド* 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!!