YouTip LogoYouTip

Python3 Os Read

# Python3.x Python3 os.read() Method [![Image 3: Python3 OS File/Directory Methods](#) Python3 OS File/Directory Methods](#) * * * ### Overview The os.read() method is used to read up to n bytes from the file descriptor fd, returning a string containing the bytes read. If the file descriptor fd points to a file that has reached the end, an empty string is returned. It is valid in 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/python3import os, sys # Open the file fd = os.open("f1.txt",os.O_RDWR) # Read the text ret = os.read(fd,12)print (ret)# Close the file os.close(fd)print ("File closed successfully!!") The output of executing the above program is: This is test File closed successfully!! [![Image 4: Python3 OS File/Directory Methods](#) Python3 OS File/Directory Methods](#) [](#)(#) (#)[](#) ### Share Notes
← Python3 Os RemovePython3 Os Read β†’