当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python os.readv()用法及代码示例


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.

代码:os.readv()方法的使用
# 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


相关用法


注:本文由纯净天空筛选整理自ihritik大神的英文原创作品 Python | os.readv() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。