Python tell() Function

The tell() function in Python, used to find the current position of the file handler or file object. Most of the time, the tell() function becomes useful to check whether the position of the file handler is at the beginning of the file or not.

Python tell() Syntax

The syntax to use the function named tell() in Python program is:

fo.tell()

where fo indicates to the file object or handler. All the details are demonstrated using examples, about this function given below.

Python tell() Example

Here is an example creates a file named myfile.txt (if not available in the current directory), and writes a line say What's Up! into the file. If the file is already available in the current directory, then any content available in the file will get replaced with What's Up!. This program uses tell() function to print the current position of the file object or handler.

fo = open("myfile.txt", "w")
text = "What's Up!"
fo.write(text)
print("The current position of file handler is:", fo.tell())
fo.close()

The output produced by above program shows in the snapshot given below:

python tell function

The text What's Up!, gets written/overwritten in the file myfile.txt. And the position of the file handler using tell produced as 10, because while writing the given content, which is of 10 characters, the file handler or pointer or object moved to the last position of the file, that is ! (exclamation mark).

Here are the list of 10 characters of the text written to the file:

  1. W
  2. h
  3. a
  4. t
  5. '
  6. s
  7. (a space)
  8. U
  9. p
  10. !

So the current position of file handler is 10. If you open the current folder, the a file myfile.txt will be available with same content written using above program. Here is the snapshot of the current directory, including the newly created file:

tell function python

Let's create another program, that demonstrates the use of tell() function:

print("Enter File's Name: ", end="")
filename = input()
fo = open(filename, "a+")
print("\nThe content of file is:", fo.read())
print("\nThe current position of file handler is:", fo.tell())
print("\nEnter the content to append: ", end="")
content = input()
fo.write("\n")
fo.write(content)
print("\nNow the current position of file handler is:", fo.tell())
print("\nAnd the new content of file is:", fo.read())
fo.close()

Here is its sample run with user input myfile.txt as file name, I'm the new content. Thank You! as content to append:

python tell function example

As you can see from the above output, because the file handler's position was changed and goes to the end of the file after appending the content to the file. So the content of file are not getting displayed. Therefore, here seek() function comes into the picture. Everything is described using the program given below.

The following statement, from above program:

fo.write("\n")

is used to insert a newline before appending the new content to the file, so that, the new content gets append from the new line.

Now let's create another program, that uses seek() function along with tell() to clear all the remaining doubt (if any) on the topic:

print("Enter File's Name: ", end="")
filename = input()
try:
    fo = open(filename, "r")
    print("\nThe current position of file handler is:", fo.tell())
    print("\nThe content of file is:", fo.read())
    print("\nAgain the current position of file handler is:", fo.tell())
    fo.close()
    print("\nEnter the content to append: ", end="")
    content = input()
    fo = open(filename, "a+")
    fo.write("\n")
    fo.write(content)
    print("\nThe current position of file handler is:", fo.tell())
    fo.seek(0)
    print("\nNow the current position of file handler is:", fo.tell())
    print("\nAnd the new content of file is:", fo.read())
except FileNotFoundError:
    print("\nThe file is not available in the current directory.")
    print("Creating the file...")
    fo = open(filename, "w+")
    print("The file is created successfully!")
    print("\nThe current position of file handler is:", fo.tell())
    print("Enter the content to write: ", end="")
    content = input()
    fo.write(content)
    print("\nNow the current position of file handler is:", fo.tell())
    fo.seek(0)
    print("\nAgain the current position of file handler is:", fo.tell())
    print("\nAnd the content of file is:", fo.read())
finally:
    fo.close()

Here is its sample run with user input myfile.txt (a file that is available in the current directory), Hi as content to append:

python tell function program

Here is another sample run with user input yourfile.txt (a non-existing file), and Hello, your file too is created. as content to write:

tell python example

After the second run of the program, a file named yourfile.txt gets created in your current directory.

Python Online Test


« Previous Function Next Function »