Python os.write() Method

Python File(文件) 方法Python OS File/Directory Methods


Overview

The os.write() method is used to write a string to the file descriptor fd. It returns the actual length of the string written.

Effective on Unix.

Syntax

write()The syntax of the method is as follows:

os.write(fd, str)

Parameters

  • fd-- file descriptor.

  • str-- the string to be written.

Return Value

This method returns the actual number of bytes written.

Example

The following example demonstrates the use of the write() method:

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

import os, sys

# 打开文件
fd = os.open("f1.txt",os.O_RDWR|os.O_CREAT)

# 写入字符串
ret = os.write(fd,"This is example.com site")

# 输入返回值
print "写入的位数为: "
print  ret

print "写入成功"

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

The output of the above program is:

写入的位数为: 
23
写入成功
关闭文件成功!!

Python File(文件) 方法Python OS File/Directory Methods

Other Extensions