logo

Python Os.write() Method


Show

Description

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

syntax

Following is the syntax for write() method :

os.write(top[, topdown=True[, onerror=None[, followlinks=False]]])

Parameters

  • fd − This is the file descriptor.
  • str − This is the string to be written.

Return Value

This method returns the number of bytes actually written.

Example

Python following example shows the usage of write() method.

# !/usr/bin/python

import os, sys

# Open file
fd = os.open("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
os.close(fd)
print "Closed the file successfully!!"

When we run the above program, it produces the following result:

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

Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.