Easy Tutorial
❮ Python Func Open Python Quadratic Roots ❯

Python3 os.read() Method

Python3 OS File/Directory Methods


Overview

The os.read() method is used to read up to n bytes from the file descriptor fd. It returns a string containing the bytes read. If the end of the file corresponding to the file descriptor fd is reached, it returns an empty string.

Valid on Unix and Windows.

Syntax

The syntax for the read() method is as follows:

os.read(fd, n)

Parameters

Return Value

Returns a string containing the bytes read.

Example

The following example demonstrates the use of 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("File closed successfully!!")

Executing the above program outputs:

This is test
File closed successfully!!

Python3 OS File/Directory Methods

❮ Python Func Open Python Quadratic Roots ❯