Python os.openpty() 方法



Python 方法 **os.openpty()** 是 OS 模組的內建函式。它開啟一個偽終端對,並返回一對不可繼承的檔案描述符,分別用於 **pty** 和 **tty**。

與 "pty" 和 "tty" 關聯的檔案描述符分別稱為 **主終端** 和 **從終端**。

當程式與 pty 互動時,它透過主檔案描述符進行通訊;而與 tty 互動時,它使用從檔案描述符。

語法

以下是 Python os.openpty() 方法的語法:

os.openpty()

引數

Python os.openpty() 方法不接受任何引數。

返回值

Python os.openpty() 方法返回一對檔案描述符,即主終端和從終端。

示例

以下示例演示了 openpty() 方法的用法,其中我們顯示了主檔案描述符和從終端名稱。

import os

# master for pty, slave for tty
m,s = os.openpty()
print("File descriptors for pty:")
print (m)
print (s)

# showing terminal name
s = os.ttyname(s)
print("File descriptors for tty:")
print (m)
print (s)

執行以上程式後,會產生以下結果:

File descriptors for pty:
3
4
File descriptors for tty:
3
/dev/pts/1
python_files_io.htm
廣告
© . All rights reserved.