Python3 os.read() 方法
概述
os.read() 方法用於從檔描述符 fd 中讀取最多 n 個位元組,返回包含讀取位元組的字串,檔描述符 fd對應檔已達到結尾, 返回一個空字元串。
在Unix,Windows中有效
語法
read()方法語法格式如下:
os.read(fd,n)
參數
fd -- 檔描述符。
n -- 讀取的位元組。
返回值
返回包含讀取位元組的字串
實例
以下實例演示了 read() 方法的使用:
#!/usr/bin/python3
import os, sys
# 打開檔
fd = os.open("f1.txt",os.O_RDWR)
# 讀取文本
ret = os.read(fd,12)
print (ret)
# 關閉檔
os.close(fd)
print ("關閉檔成功!!")
執行以上程式輸出結果為:
This is test 關閉檔成功!!

Python3 OS 檔/目錄方法