當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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

相關用法


注:本文由純淨天空篩選整理自 Python 3 - os.ttyname() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。