Python3 Os Read
# Python3.x 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 file descriptor fd corresponds to a file that has reached the end, an empty string is returned.
Valid in Unix and Windows.
### Syntax
The syntax for the **read()** method is as follows:
os.read(fd,n)
### Parameters
* **fd** -- The file descriptor.
* **n** -- The number of bytes to read.
### Return Value
Returns a string containing the bytes read.
### Example
The following example demonstrates the use of the read() method:
#!/usr/bin/python3import 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!!")
The output of executing the above program is:
This is test File closed successfully!!
[ Python3 OS File/Directory Methods](#)
YouTip