File Handling : File Pointer

Data Files:

  1. Text Files

Python provides built-in functions to perform read , write and append data to files.

Working with Text Files:

Reading from Text Files:

  1. read( )
  2. readline( )
  3. 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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response