Os Read
# Python2.x Python os.read() Method
[ Python OS File/Directory Methods](#)
* * *
### Overview
The os.read() method is used to read up to n bytes from a file descriptor fd, returning 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 on Unix and Windows.
### Syntax
The syntax for the **read()** method is as follows:
os.read(fd,n)
### Parameters
* **fd** -- File descriptor.
* **n** -- 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/python# -*- coding: UTF-8 -*-import 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!!
[ Python OS File/Directory Methods](#)
YouTip