Readp() function of the os module in Python

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:

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

b'Merrily merrily, merrily, merrily\n'

 


Copyright 2024 © pythontic.com