File Handling: ============== tell(): ======= ==> an inbuilt method in python which we can use to get the current location of the file-pointer/file-object from the beginning of the file. Syntax: file-pointer.tell() # tell() fp = open("abc.txt","r+") print("The Position of fp = ",fp.tell()) fp.read(5) print("The Position of fp = ",fp.tell()) fp.read(10) print("The Position of fp = ",fp.tell()) fp.write("Continue.") fp.readline() print("The Position of fp = ",fp.tell()) =============================================== seek(): ======= ==> is an inbuilt method which we can use to move the file pointer from one position to another position. Syntax: file-object.seek(offset, from-where) here: offset ==> number of positions, that how many need to move from-where ==> the start point to define the move from-where: 0 ==> from the beginning of the file 1 ==> from the current position. 2 ==> from the end of the file. Note: ==== python-3 can allowed to define the seek() for moving the file pointer from the beginning by default. from-where with the values '1' and '2' ==> not supported in python-3. # seek() data = "Here is the Python Full Stack Content." fp = open("abc.txt","w") fp.write(data) with open("abc.txt","r+") as fp: text = fp.read() print(text) print("The Current Position of fp = ",fp.tell()) fp.seek(17) print("The Current Position of fp = ",fp.tell()) fp.seek(0) print("The Current Position of fp = ", fp.tell()) fp.seek(20) print("The Current Position of fp = ", fp.tell()) ============================================ Checking of the file whether it is exists or not? ================================================= import os f = input("Enter the file name:") if os.path.isfile(f): print("File is Existed.") else: print("File is not Existed.") =========================================== Q: WAP TO CHECK WHETHER THE FILE IS EXISTED OR NOT. IF THE FILE IS AVAILABLE, THEN PRINT ITS CONTENT. import os,sys fname = input("Enter the file to search:") if os.path.isfile(fname): print("File is Existed:",fname) fp = open(fname,"r") else: print("File Does not Exist:",fname) sys.exit(0) print("The Content of file is = ") data = fp.read() print(data) fp.close() ======================================================== Assignment: =========== WAP TO PRINT THE NUMBER OF LINES, WORDS AND CHARACTERS PRESENT IN THE FILE. ======================================== HANDLING OF BINARY FILES: ========================= MODES FOR BINARY FILES TO HANDLE: r, w, a, r+, w+, a+, x ==> Text file modes rb, wb, ab, r+b, w+b, a+b, xb ==> Binary file modes # WAP TO READ IMAGE FILE AND WRITE TO A NEW IMAGE FILE. f1 = open("image1.jpg","rb") f2 = open("image2.jpg","wb") bytes = f1.read() f2.write(bytes) print("The New Image has created from Image1 Successfully.") =====================================================