File Handling : File Pointer

Data Files:
- Text Files
Python provides built-in functions to perform read , write and append data to files.
Working with Text Files:
Reading from Text Files:
- read( )
- readline( )
- readlines( )
Input Text File


Output:

Code Explanation:
Statement 1: file1 is file-handle(also called file object) is a reference to a file which allows to perform different tasks.
Statement 2:Reads 10 bytes now str1 has data hello worl now the file pointer is at byte d
Statement 3:prints the data stored in the variable str1 hello worl
Statement 4: Reads 2 bytes now file pointer is at 11 byte (d) from this point it reads 2 bytes i.e.. dw
Statement 5: prints the data stored in the variable str2 dw
Statement 6: Reads a line to input now file pointer is at 12th byte from this point it reads entire line. str3 is stored with data elcome to python class
Statement 7: prints the data stored in the variable str3 elcome to python class
Statement 8: Reads 1 byte now the file pointer is at end of second line so it reads first byte in the third line h. str4 is stored with data h
Statement 9: prints the data stored in the variable str4 h
Statement 10: Reads all the lines and returns them in a list. So remaining data in the text file appy coding is stored in the variable str5
Statement 11: prints the data stored in the variable str5 appy coding
Statement 12: close() function breaks the link of file-object and the file on the disk.