+

Python | os.pwrite () method

os.pwrite() in Python is used to write a specified test line to a file associated with a specified file descriptor at a specified position. Any existing value will be overwritten at the specified position.

File descriptor — it is a small integer value that corresponds to a file that was opened by the current process. It is used to perform various lower level I / O like read, write, send, etc.

Note: The os.pwrite () method is designed to work low-level and should be applied to the file descriptor returned by os.open () or os.pipe() method.

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

Parameters:
fd : A file descriptor representing the file to be written.
str : A bytestring representing the content to be written in the file
offset : An integer value denoting the starting position. File writing will start from this offset value.

Return Type: This method returns an integer value representing the number of bytes actually written. .

Consider the following text as the content of a file named file.txt .

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

Code: using the os.pwrite () method

# Python program to explain the os.pwrite () method

 
# Import os module

import os

 
# File name

filename = "file.txt"

 
# Open file and get
# the file descriptor is linked
# using the os.op method en

fd = os. open (filename, os.O_RDWR)

 
# Line to write to file

s = "Python,"

 
# convert string to byte string

text = s.encode ( "utf-8" )

 
# Location from where
# recording will start file

offset = 0

 
# Offset is 0, byte string
# will be written in
# start of file

 
# Write byte string
# to the file specified
# file descriptor in
# specified position

bytesWritten = os.pwrite (fd, text, offset)

print ( "Number of bytes actually written:" , bytesWritten)

 
# Print file content

with open (filename) as f: 

print (f.read ( ))

 
# String to write to file

s = "Javascript,"

 
# convert string to byte string

text = s.encode ( "utf-8 " )

  
# Location from where
# file will start writing
# os.stat (fd) .st_size will return
# file size in bytes
# so the line will be written # at the end of the file

offset = os.stat (fd) .st_size

 
# Write a byte string
# to the file specified
# file descriptor in
# specified position

bytesWritten = os.pwrite (fd, text, offset)

print ( "Number of bytes actually written:" , bytesWritten)

 
# Print file content

with open (filename) as f:

  print (f.read ())

 
# Line to write to file

s = "R Programming,"

 
# convert string to byte string

text = s.encode ( "utf-8" )

  
# Location from where
# file will start writing

offset = 10

 
# Write a byte string  > # to the file specified
# file descriptor in
# specified position

bytesWritten = os. pwrite (fd, text, offset)

print ( " Number of bytes actually written: " , bytesWritten)

  
# Print file content

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, Ja  R Programming,  ascript, 
Get Solution for free from DataCamp guru