seek() function in python by R4R Team

- seek() is a python function which set the file current location at the offset.

Syntax-

fileobject.seek(offset)

-Here fileobject is the object which points to the file location.
-offset is the numeric value,read/write operation starting from this position.


program-

#open a file and write
f=open('myfile.txt','w')
f.write("This is my data inside the file")
f.close()

#read file
f=open('myfile.txt','r')
print(f.read())

#open file and read with seek(11)
f=open('myfile.txt','r')
f.seek(11)
print(f.read())

#open file and read with seek(5)
f=open('myfile.txt','r')
f.seek(5)
print(f.read())

#open file and read with seek(20)
f=open('myfile.txt','r')
f.seek(20)
print(f.read())

#close file
f.close()


output-

This is my data inside the file
data inside the file
is my data inside the file
de the file


-In this program, we firstly write a file and then read that file,
-seek(11) with read() , it read the text at 11th index so starting text are not shown in the output, and similary other seek(index) works.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!