You are here : python_2ososwrite

os.write() - os


The method write() writes the string str to file descriptor fd. Return the number of bytes actually written.

  • fd -- This is the file descriptor.

  • str -- This is the string to be written.


os.write(fd, str)


# !/usr/bin/python

import os, sys

# Open file
fd ="f1.txt",os.O_RDWR|os.CREAT)

# Writing text
ret = os.write(fd,"This is test")

# ret consists of number of bytes written to f1.txt
print "the number of bytes written: "
print  ret

print "written successfully"

# Close opened file
print "Closed the file successfully!!"

Output / Return Value

When we run above program, it produces following result:

the number of bytes written: 12 written successfully Closed the file successfully!!


Alternatives / See Also