Python os.read() Method
os.read() Overview
The os.read() method is used to read up to n bytes from the file descriptor fd, returning a string containing the bytes read, with the file descriptor fd corresponding to the end of the file, and returning an empty string.
Valid in Unix, Windows
os.read() Syntax
The syntax format of the read() method is as follows.
os.read(fd,n)
os.read() Parameters
- fd – The file descriptor.
- n – The bytes to be read.
os.read() Return Value
Returns a string containing the read bytes
os.read() Examples
The following example demonstrates the use of the read() method.
#!/usr/bin/python3
import os, sys
# open file
fd = os.open("/root/apidemos.com.txt",os.O_RDWR)
# read text
ret = os.read(fd,12)
print (ret)
# close file
os.close(fd)
print ("Close file successfully!!!")
Output: