File operations for tell() and seek() functions in Python

A Python program to demonstrate file operations for tell(), seek() functions and copying content from one file to another.

In this tutorial, we’ll be learning the file operations like tell() and seek() and also will learn, how to copy contents from one file to another. Before getting into the program let us see some common function and mode in file.

Firstly, open() is a function that helps us to access the file according to the user’s needs.

Inside the open() we give the file name and the mode of the file. For example: open(“Hello.txt”,”w”) .

Here in the above example we have mentioned the file name(Hello.txt) and mode(W) within the

double quotes and separated by comma. Later we see write() function, in this, we write the content into the file.

Explanation of the program:

Now let us dive into the program. In the first block, we are creating a file named texting.txt and writing the contents into the file using write(). Once the writing job is done and if we want to see or read the file then we make use of “r” mode and readline()/readlines() [based on the user’s convenience].

In the second block, we use tell() function to get the current position of the word in the file. Later in the program i.e, in the third block we will reposition the pointer to the beginning of the file from the previous pointer. This can be done using seek() function.

Now let us see how we can copy content from one file to another. To make this happen we make use of with statement which is used to simplify exception handling by encapsulating common preparation and to cleanup the task. In that last block of the program, we create an object by using as and give an aliasing name for the file.

When we say:  with open(“texting.txt”)as fa:   with open(“out1.txt”,”w”)as fa1:   for line in fa:   fa1.write(line)

We’ll be copying all the contents present in the file texting.txt to the file out1.txt.

 

#To create a new file and writing into the file

fob=open("texting.txt","w")
fob.write("This is the first line \n This is the second line")
fob=open("texting.txt")
line=fob.readline()
print"read line %s",%(line)

#To get the current position of the file 

position=fob.tell()
print("Current position %d" %(position))
print("\n")

#Repositioning pointer at the begining once again

position1=fob.seek(0)
ran=fob.read(10)
print("Repositioning the file pointer to the begining of the file",ran)
print("\n")

#copying contents of one file to another.

with open("texting.txt")as fa:
    with open("out1.txt","w")as fa1:
        for line in fa:
            fa1.write(line)
    print("contents are copied successfully")

File operations for tell() and seek() functions in Python

3 responses to “File operations for tell() and seek() functions in Python”

  1. Adithya Parashar says:

    Very informative !

  2. prajwal.n.n says:

    very informative and easy to understand

  3. Enigma says:

    Helped a lot!!

Leave a Reply

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