Open In App

Python | os.readv() method

Improve
Improve
Like Article
Like
Save
Share
Report

OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.

os.readv() method in Python is used to read data from a file indicated by the specified file descriptor into a number of specified buffers. Here, buffers are sequence of mutable bytes-like objects. This method read data from the file descriptor and transfer read data into each buffer until it is full and then move on to the next buffer in the sequence to hold the rest of the data.

A file descriptor is small integer value that corresponds to a file that has been opened by the current process. It is used to perform various lower level I/O operations like read, write, send etc.

Note: os.readv() method is available only on UNIX platforms.

Syntax: os.readv(fd, buffers)

Parameters:
fd: A file descriptor indicating the file to be read.
buffers: A sequence of mutable bytes-like objects. The read data will be transferred into these bytes-like objects.

Return Type: This method returns an integer value which represents the number of bytes actually read. Its value can be less than or equal to the total capacity of all the objects.

Consider the below text as the content of the file named 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.

Code: Use of os.readv() method




# 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)


Output:

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


Last Updated : 29 Jun, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads