Tuesday, August 11, 2020

Python os.read() Method with example



Hello dear readers! Welcome back to another section of my tutorial on Python. In this tutorial guide, we are going to be studying about the Os read() method.

Python os read() method reads at most n bytes from file descriptor fd, returns a string containing the bytes read. If the end of the file referred to by fd has been reached, an empty string is returned.

Syntax

The following below is the syntax for Python Os read() method -

os.read(fd,  n)


Parameter Details

  • fd - This is the file descriptor of the file.
  • n - n bytes from the file descriptor fd.

Return Value

It returns a string that contains the bytes read.

Example

The following below is a simple example -

# !/usr/bin/python

import os, sys
# Open a file
fd = os.open("f1.txt",os.O_RDWR)
 
# Reading text
ret = os.read(fd,12)
print ret

# Close opened file
os.close(fd)
print "Closed the file successfully!!"

Output
When the above code is executed, it will produce the following result -

This is test
Closed the file successfully!!


Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial, we are going to be discussing about the Python OS readlink() Method.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.