Have you ever wanted to know what is seek() and tell() functions in python and the difference between them?
The tell function is used to determine the current position of the file pointer “file.tell()”, and the seek function is used to move the file pointer to the specified position of the file “file.seek(0, 0)”. Both functions are important tools for managing file objects in python
Python is a great language for getting the job done. So, if you’re looking to get started in programming, Python is a great place to start. The methods in this scripting language are definitely one of these features.
Methods are functions that are associated with objects. In Python, every object has its own set of methods that can be used to perform various operations on that object. For example, the list object has methods that can be used to add, remove or sort items in the list.
Methods are called using dot notation. For example, if we have a list named mylist, we can call the append() method like this: mylist.append(item).
In addition to the methods that are associated with objects, there are also built-in methods that can be used. seek() and tell() function is one among them.
In this blog post, we’ll explore the differences between the seek and tell functions in Python and how they can be used in your programming. By the end of this post, you should have a better understanding of the seek-and-tell functions and their applications in Python.
seek () function in Python
Python’s `seek () ` function is used to move the position of the file pointer in a file, It takes 2 arguments seak(offset,from_what)
The `seek () ` function receives two arguments:
1. The first is the offset argument which specifies the number of bytes to move the pointer.
2. The from_what argument is the second argument that specifies the reference point for the offset.
In Python, the syntax for the seek function is:
file_object.seek(offset, from_what), file_object is the name of the file you want to seek within.
- offset: The number of bytes you want to move the file pointer
- from_what: Where you want to start counting from.
The from_what argument can be,
- 0(which means to start counting from the beginning of the file).
- 1 (which means to start counting from the current position of the file pointer)
- 2 (which means to start counting from the end of the file).
NOTE: If you don’t specify the whence argument, it defaults to 0.
For example, to move the file position 5 bytes from the start of the file, you would use `seek (5, 0) `. To move the file position 5 bytes from the current position, you would use `seek (5, 1) `. And to move the file position 5 bytes from the end of the file, you would use `seek (5, 2) `.
Example Program 1:
# Open the file f = open('file.txt', 'r+') # Move the read/write pointer to the beginning of the file f.seek(0) # Read the file contents = f.read() # Print the contents print(contents) # Close the file f.close()
Example Program 2:
A file named “file.txt” contains the text “Python is a versatile scripting language”.
# Python program to demonstrate seek() method # Opening "file.txt" text file f = open("file.txt", "r") # The Second parameter is by default 0 # Sets Reference point to 10. # Index position from the beginning f.seek(10) # Prints current position print(f.tell()) print(f.readline()) f.close()
a versatile scripting language
tell() function in Python
tell() function is a useful tool that allows you to get the current position of the file pointer. This is useful when you want to keep track of your position in a file, or if you want to seek a specific position in a file. The tell function returns an integer value that represents the current position of the file pointer.
The tell function has the following syntax:
fileObject.tell(fd), where fd is the file descriptor of the open file.
Example Program 1:
A file named “file.txt” contains the text “Python is a versatile scripting language.”
# Python program to demonstrate tell() method # Open the file in read mode fp = open("myfile.txt", "r") # Print the position of handle print(fp.tell()) #Closing file fp.close()
Example Program 2:
# Python program to demonstrate tell() method # Opening file fp = open("file.txt", "r") fp.read(5) # Print the position of handle print(fp.tell()) # Closing file fp.close()
Difference Between seek() and tell() Function
The two most important functions for working with files in Python are the seek() and tell() functions. These functions are used to move the cursor in a file so that you can read or write data at specific locations.
The main difference between these two functions is that seek() allows you to specify an absolute position in a file, while tell() returns the current position of the cursor.
Here is a more detailed look at these two functions:
There are a few other differences between seek() and tell():
– seek() takes an argument that specifies the position to move the file pointer to, while tell() doesn’t take any arguments.
– seek() moves the file pointer forward or backward, whereas tell() can only be used to find the current position of the file pointer.
– seek() can be used on both text files and binary files
Python provides a great way to improve your programming skills through the functions that are constantly being updated. They are designed to make working with these data structures easier and more efficient. To stay up-to-date on the latest in programming, like, follow, and comment on our blog.
Good Luck with your Learning !!