Python os.pwrite() – Writes the specified byte string to the specified file descriptor

Python os.pwrite()

The Python os.pwrite() method is used to write the specified byte string to the specified location in the file associated with the specified file descriptor. Any existing value will be overwritten at the specified location.

A file descriptor is a small integer value corresponding to a file currently open by the process. It is used to perform various low-level I/O operations such as reading, writing, and sending.

Note: The os.pwrite() method is used for low-level operations and should be applied to file descriptors returned by the os.open() or os.pipe() methods.

Syntax: os.pwrite(fd, str, offset)

Parameters:

fd: The file descriptor representing the file to be written.

A byte string representing the contents to be written to the file.

offset: An integer indicating the starting position. Writing to the file will begin at this offset.

Return type: This method returns an integer indicating the number of bytes actually written.

Treat the following text as the contents of a file named file.txt.

C, C++, Java, C#, PHP

Example 1

Using the os.pwrite() method

# Python program to explain os.pwrite() method
  
# Importing os module
import os
  
#Filename
filename = "file.txt"
  
# Open the file and get the
# file descriptor associated
# with it using os.open method
fd = os.open(filename, os.O_RDWR)
  
# String to be written in the file
s = "Python, "
  
# converting string to bytestring
text = s.encode("utf-8")
  
# Position from where
# file writing will start
offset=0
  
# As offset is 0, bytestring
# will be written in the
#beginning of the file
  
# Write the bytestring
# to the file indicated by
# file descriptor at
#specified position
bytesWritten = os.pwrite(fd, text, offset)
print("Number of bytes actually written:", bytesWritten)
  
# Print the content of the file
with open(filename) as f:
Print(f.read())
  
# String to be written in the file
s = "Javascript, "
  
# converting string to bytestring
text = s.encode("utf-8")
  
# Position from where
# file writing will start
# os.stat(fd).st_size will return
# file size in bytes
# so bytestring will be written
# at the end of the file
offset = os.stat(fd).st_size
  
# Write the bytestring
# to the file indicated by
# file descriptor at
#specified position
bytesWritten = os.pwrite(fd, text, offset)
print("nNumber of bytes actually written:", bytesWritten)
  
# Print the content of the file
with open(filename) as f:
Print(f.read())
  
# String to be written in the file
s = "R Programming, "
  
# converting string to bytestring
text = s.encode("utf-8")
  
# Position from where
# file writing will start
offset=10
  
# Write the bytestring
# to the file indicated by
# file descriptor at
#specified position
bytesWritten = os.pwrite(fd, text, offset)
print("nNumber of bytes actually written:", bytesWritten)
  
# Print the content of the file
with open(filename) as f:
Print(f.read())

Output:

Number of bytes actually written: 8
Python, Java, C#, PHP

Number of bytes actually written: 12
Python, Java, C#, PHP
Javascript,

Number of bytes actually written: 15
Python, JaR Programming, ascript,

Leave a Reply

Your email address will not be published. Required fields are marked *