readp() function of the os module in Python

The readp() function of the os module in Python reads specified number of bytes or less from a given offset.

Os Updated 3 May 2023 1 min read

Overview

  • The function os.pread() reads specified number of bytes or less starting from a specified position at a file.
  • It returns an empty bytes object when end of file is reached.

Example

python
# Example Python program that reads

            # from a specific position on a file

            # using the function os.pread()

            import os

# Open the file that has the text

            # of the song "row row row your boat..."

            flags     = os.O_RDONLY

            mode     = 0o744

            fd         = os.open("/ex/RowYourBoat.txt", flags, mode)

# Read the whole line that starts with Merrily

            readFrom     = 47

            bytesCount    = 34

bytesRead = os.pread(fd, bytesCount, readFrom)

# Print the read bytes

            print(bytesRead)

Output

Output
b'Merrily merrily, merrily, merrily\n'