Hello dear readers! Welcome back to another section of our 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.
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)
RECOMMENDED POST: Python os.popen() Method with example
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!!
RECOMMENDED POST: Python os.dup() method with example
Alright guys! This is where we are rounding up for this tutorial post. In our 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.
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.