Python3 os.read() Method

Python3 OS 文件/目录方法Python3 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, an empty string is returned.

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 usage of the read() method:

#!/usr/bin/python3

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

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

The output result of executing the above program is:

This is test
关闭文件成功!!

Python3 OS 文件/目录方法Python3 OS File/Directory Methods

Other Extensions