Python中的OS模塊提供了與操作係統進行交互的函數。操作係統屬於Python的標準實用程序模塊。該模塊提供了使用依賴於操作係統的函數的便攜式方法。
os.readv()
Python中的方法用於將數據從指定文件描述符所指示的文件中讀取到多個指定緩衝區中。在這裏,緩衝區是可變bytes-like對象的序列。此方法從文件描述符中讀取數據,並將讀取的數據傳輸到每個緩衝區中,直到緩衝區滿為止,然後移至序列中的下一個緩衝區以保留其餘數據。
文件描述符是一個小整數值,對應於當前進程已打開的文件。它用於執行各種較低級別的I /O操作,例如讀取,寫入,發送等。
注意:os.readv()
該方法僅在UNIX平台上可用。
用法: os.readv(fd, buffers)
參數:
fd:表示要讀取文件的文件描述符。
buffers:一係列可變的bytes-like對象。讀取的數據將被傳輸到這些bytes-like對象。
返回類型:此方法返回一個整數值,該整數值表示實際讀取的字節數。它的值可以小於或等於所有對象的總容量。
將以下文本視為名為Python_intro.txt的文件的內容。
Python is a widely used general-purpose, high level programming language. It was initially designed by Guido van Rossum in 1991 and developed by Python Software Foundation. It was mainly developed for emphasis on code readability, and its syntax allows programmers to express concepts in fewer lines of code. Python is a programming language that lets you work quickly and integrate systems more efficiently.
# Python program to explain os.readv() method
# import os module
import os
# File path
path = "./file.txt"
# Open the file and get the
# file descriptor associated
# with it using os.open() method
fd = os.open(path, os.O_RDONLY)
# Bytes-like objects to hold
# the data read from the file
size = 20
buffer1 = bytearray(size)
buffer2 = bytearray(size)
buffer3 = bytearray(size)
# Read the data from the
# file descriptor into
# bytes-like objects
# using os.readv() method
numBytes = os.readv(fd, [buffer1, buffer2, buffer3])
# Print the data read in buffer1
print("Data read in buffer 1:", buffer1.decode())
# Print the data read in buffer2
print("Data read in buffer 2:", buffer2.decode())
# Print the data read in buffer3
print("Data read in buffer 3:", buffer3.decode())
# Print the number of bytes actually read
print("\nTotal Number of bytes actually read:", numBytes)
Data in buffer 1:Python is a widely u Data in buffer 2:sed general-purpose, Data in buffer 3: high level programm Total Number of bytes actually read:60
相關用法
- Python next()用法及代碼示例
- Python os.dup()用法及代碼示例
- Python set()用法及代碼示例
- Python Decimal max()用法及代碼示例
- Python PIL ImageOps.fit()用法及代碼示例
- Python os.rmdir()用法及代碼示例
- Python sympy.det()用法及代碼示例
- Python Decimal min()用法及代碼示例
- Python os.readlink()用法及代碼示例
- Python os.writev()用法及代碼示例
- Python PIL RankFilter()用法及代碼示例
- Python os.rename()用法及代碼示例
- Python os.sendfile()用法及代碼示例
- Python os.makedev()用法及代碼示例
注:本文由純淨天空篩選整理自ihritik大神的英文原創作品 Python | os.readv() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。