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)

 

 

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *