close
The Wayback Machine - https://web.archive.org/web/20240812003930/https://www.geeksforgeeks.org/python-os-read-method/
Open In App

Python | os.read() method

Last Updated : 18 Jun, 2019
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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




# Python program to explain os.read() method 
    
# importing os module 
import os
  
# File path 
path = "/home / ihritik / Documents / Python_intro.txt"
  
  
# Open the file and get
# the file descriptor associated
# with it using os.open() method
fd = os.open(path, os.O_RDONLY)
  
  
# Number of bytes to be read
n = 50
  
# Read at most n bytes 
# from file descriptor fd
# using os.read() method
readBytes = os.read(fd, n)
  
# Print the bytes read
print(readBytes)
  
# close the file descriptor
os.close(fd)


Output:

b'Python is a widely used general-purpose, high leve'

Previous Article
Next Article

Similar Reads

Python - Read blob object in python using wand library
BLOB stands for Binary Large OBject. A blob is a data type that can store binary data. This is different than most other data types used in databases, such as integers, floating point numbers, characters, and strings, which store letters and numbers. BLOB is a large complex collection of binary data which is stored in Database. Basically BLOB is us
2 min read
Read a Particular Page from a PDF File in Python
Document processing is one of the most common use cases for the Python programming language. This allows the language to process many files, such as database files, multimedia files and encrypted files, to name a few. This article will teach you how to read a particular page from a PDF (Portable Document Format) file in Python. Method 1: Using Pymu
4 min read
Python - Read CSV Column into List without header
Prerequisites: Reading and Writing data in CSV CSV files are parsed in python with the help of csv library. The csv library contains objects that are used to read, write and process data from and to CSV files. Sometimes, while working with large amounts of data, we want to omit a few rows or columns, so that minimum memory gets utilized. Let's see
2 min read
Python | OpenCV program to read and save an Image
OpenCV (Open Source Computer Vision) is a library of programming functions mainly aimed at real-time computer vision. This is cross-platform library, it provides functions that are used in multiple languages. Coming to image processing, OpenCV enables us to perform multiple operations on image, but in order to do that we need to read an image file
2 min read
How to read from a file in Python
Python provides inbuilt functions for creating, writing and reading files. There are two types of files that can be handled in python, normal text files and binary files (written in binary language, 0s and 1s). Text files: In this type of file, Each line of text is terminated with a special character called EOL (End of Line), which is the new line
5 min read
Read, Write and Parse JSON using Python
JSON is a lightweight data format for data interchange that can be easily read and written by humans, and easily parsed and generated by machines. It is a complete language-independent text format. To work with JSON data, Python has a built-in package called JSON. Example of JSON String s = '{"id":01, "name": "Emily", "language": ["C++", "Python"]}
4 min read
Python program to read character by character from a file
Python is a great language for file handling, and it provides built-in functions to make reading files easy with which we can read file character by character. In this article, we will cover a few examples of it. Example Input: GeeksOutput: G e e k sExplanation: Iterated through character by character from the input as shown in the output.Read a fi
2 min read
Python program to read CSV without CSV module
CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the nam
3 min read
Python program to read file word by word
Python is a great language for file handling, and it provides built-in functions to make reading files easy with which we can read file word by word. Read file word by wordIn this article, we will look at how to read a text file and split it into single words using Python. Here are a few examples of reading a file word by word in Python for a bette
2 min read
Read Modifiers in Wand Python
Read modifier refers to immediately modify input image or file format after reading or to make the image perfect to manipulate just after reading the image. This can be done by using the filename parameter of Image() function. For instance, let us say we want to read an image of (100 x 100) dimensions/aspect ratio but the original image is of (200
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg