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

Python3 os.read() Method

Python3 os.read() Method

Python3 OS File/Directory Methods Python3 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/python3

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
关闭文件成功!!

Python3 OS File/Directory Methods Python3 OS File/Directory Methods