Site icon DataFlair

Python File tell() and seek() Method

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

Program 1

# Program for tell() and seek()
import os
try:
    if(os.path.isfile("d://indore/student1.txt")):
          f=open("d://indore/student1.txt","r")
        #   print(f.tell()) 
        #   print(f.read(5))
        #   print(f.tell())
          f.seek(2,0)
          print(f.tell()) 
          print(f.read(5))
        #   f.seek(0,0)
        #   print(f.tell()) 
        #   print(f.read(5))
        #   f.seek(0,1)
        #   print(f.tell()) 
        #   print(f.read(5))
          f.close()
    else:
        print("File not found")    
except Exception as obj:
     print(obj)

 

 

Exit mobile version