Latest web development tutorials

Python3 os.read () method

Python3 OS file / directory methods Python3 OS file / directory methods


Outline

os.read () method is used to read from the file descriptor fd up to n bytes and returns a string containing the bytes read, the corresponding file descriptor fd has reached the end, returns an empty string.

Valid in Unix, Windows in

grammar

read () method syntax is as follows:

os.read(fd,n)

parameter

  • fd - file descriptor.

  • n - bytes read.

return value

Returns a string containing the bytes read

Examples

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

#!/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 above program output is:

This is test
关闭文件成功!!

Python3 OS file / directory methods Python3 OS file / directory methods