Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python os.write() method

Python os.write() method

Python File methods Python OS file/directory methods


Overview

The os.write() method writes a string to the file descriptor fd and returns the actual length of the string written.

Valid on Unix.

Syntax

The **write()** method syntax is as follows:


os.write(fd, str)

Parameters

Return Value

This method returns the actual number of bytes written.

Example

The following example shows how to use the write() method:


#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

# Open file
fd = os.open("f1.txt",os.O_RDWR|os.O_CREAT)

# Write string
ret = os.write(fd,"This is runoob.com site")

# Input return value
print "写入的位数为: "
print  ret

print "写入成功"

# Close file
os.close(fd)
print "关闭文件成功!!"

The output of the above program is:


Number of bits written:
23
写入成功
关闭文件成功!!

Python File methods Python OS file/directory methods