Python | os.read() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.
os.read()
method in Python is used to read at most n bytes from the file associated with the given file descriptor.
If the end of the file has been reached while reading bytes from the given file descriptor, os.read()
method will return an empty bytes object for all bytes left to be read.
A file descriptor is small integer value that corresponds to a file that has been opened by the current process. It is used to perform various lower level I/O operations like read, write, send etc.
Note: os.read()
method is intended for low-level operation and should be applied to a file descriptor as returned by os.open()
or os.pipe()
method.
Syntax: os.read(fd, n)
Parameter:
fd: A file descriptor representing the file to be read.
n: An integer value denoting the number of bytes to be read from the file associated with the given file descriptor fd.
Return Type: This method returns a bytestring which represents the bytes read from the file associated with the file descriptor fd.
Consider the below text as the content of the file named Python_intro.txt.
Python is a widely used general-purpose, high level programming language. It was initially designed by Guido van Rossum in 1991 and developed by Python Software Foundation. It was mainly developed for emphasis on code readability, and its syntax allows programmers to express concepts in fewer lines of code. Python is a programming language that lets you work quickly and integrate systems more efficiently.
Code: Use of os.read() method to read n bytes from a given file descriptor
import os
path = "/home / ihritik / Documents / Python_intro.txt"
fd = os. open (path, os.O_RDONLY)
n = 50
readBytes = os.read(fd, n)
print (readBytes)
os.close(fd)
|
Output:
b'Python is a widely used general-purpose, high leve'
Last Updated :
18 Jun, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...