Python | os.pread() method

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.pread() method in Python is used to read at most n bytes from the file associated with the given file descriptor at a position of given offset value.

If the end of the file has been reached while reading bytes from the given file descriptor, os.pread() method will return an empty bytes object for all bytes left to be read and also it does not affect the file offset value.



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.pread() method is intended for low-level operation and should be applied to a file descriptor as returned by os.open() or os.pipe() method.

Syntax: os.pread(fd, n, offset)

Parameters:
fd: A file descriptor representing the file to be read.
n: An integer value denoting the number of bytes to be read from the file associated with the given file descriptor fd.
offset: An integer value denoting the offset bytes.

Return Type: This method returns a bytestring which represents the bytes read from the file associated with the file descriptor fd at a position of given offset value.

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.pread() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain os.pread() method 
    
# importing os module 
import os
  
  
# Open the file and get
# the file descriptor associated
# with it using os.open() method
fd = os.open("Python_intro.txt", os.O_RDONLY)
  
  
# Number of bytes to be read
n = 50
  
# Read at most n bytes from 
# file descriptor fd
# using os.read() method
readBytes = os.read(fd, n)
  
# Print the bytes read
print(readBytes)
  
  
# Now set the Offset value 
offset = 20
   
# Read at most n bytes from 
# file descriptor fd at a position of
# given offset value using os.pread() method
readBytes = os.pread(fd, n, offset)
  
# Print the bytes read
print(readBytes)
  
# close the file descriptor
os.close(fd)

chevron_right


Output:

b'Python is a widely used general-purpose, high leve'
b'sed general-purpose, high level programming langua'


My Personal Notes arrow_drop_up

self motivated and passionate programmer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.