Python os.read() Method

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


Overview

The os.read() method is used to read at most n bytes from the file descriptor fd, returning a string containing the read bytes. If the file corresponding to the file descriptor fd has reached the end, it returns an empty string.

Valid on Unix and Windows

Syntax

read()The method syntax is as follows:

os.read(fd,n)

Parameters

  • fd-- File descriptor.

  • n-- Number of bytes to read.

Return Value

Returns a string containing the read bytes

Example

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

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

import os, sys
# 打开文件
fd = os.open("f1.txt",os.O_RDWR)
    
# 读取文本
ret = os.read(fd,12)
print ret

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

The output of the above program is:

This is test
关闭文件成功!!

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

Other Extensions