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

Python os.read() method

Python os.read() method

Python File methods Python OS file/directory methods


Overview

The os.read() method reads at most n bytes from the file descriptor fd and returns a string containing the bytes read; if the file corresponding to fd has reached its end, it returns an empty string.

Valid on Unix and Windows

Syntax

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


os.read(fd,n)

Parameters

Return Value

Returns a string containing the bytes read

Example

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


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

import os, sys
# Open file
fd = os.open("f1.txt",os.O_RDWR)
    
# Read text
ret = os.read(fd,12)
print ret

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

The output of the above program is:


This is test
关闭文件成功!!

Python File methods Python OS file/directory methods