Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

I just need to open a file and read each and every line by using f.seek() and f.tell():

test.txt

abc

def

ghi

jkl

Code:

f = open('test.txt', 'r')

last_pos = f.tell()  # get to know the current position in the file

last_pos = last_pos + 1

f.seek(last_pos)  # to change the current position in a file

text= f.readlines(last_pos)

print text

It actually reads the whole file

closed

4 Answers

0 votes
by (25.7k points)
selected by
 
Best answer
The f.seek() and f.tell() methods are typically used to manipulate the current position within a file, but they are not necessary for reading each line of a file sequentially.

To read each line of a file, you can use a loop with the readline() method. Here's an updated version of your code to achieve that:

f = open('test.txt', 'r')

for line in f.readlines():

    print(line)

f.close()

In this code, f.readlines() returns a list containing all the lines in the file. The loop then iterates over each line and prints it. Finally, don't forget to close the file using f.close() to release system resources.

Alternatively, you can use a more concise and recommended approach using a with statement, which automatically takes care of closing the file for you:

with open('test.txt', 'r') as f:

    for line in f.readlines():

        print(line)

This with statement ensures the file is properly closed even if an exception occurs during the file operations.
0 votes
by (26.4k points)

Kindly check the below code:

f = open( ... )

f.seek(last_pos)

line = f.readline()  # no 's' at the end of `readline()`

last_pos = f.tell()

f.close()

simply recall, last_pos isn't a line number in your document, it's a byte offset from the start of the file - there's no reason for incrementing/decrementing it.

Interested to learn python in detail? Come and Join the python course.

0 votes
by (15.4k points)

Here's an  way to read each line of a file without using readlines(): 

with open('test.txt', 'r') as f: 

for line in f: 

print(line.strip())

In this approach, the file object f itself is iterable, so you can iterate over it directly in the for loop. Each iteration will provide a single line from the file, which can be processed as needed. The strip() method is used to remove any leading or trailing whitespace characters from each line before printing. Using this method, you can achieve the same result of reading and printing each line of the file in a more concise and efficient manner.

0 votes
by (19k points)

with open('test.txt', 'r') as file:

    for line in file:

        print(line.strip())

In this approach, the file is opened using a with statement, which automatically takes care of closing the file once the block of code is executed. The file object file itself is treated as an iterable, allowing you to iterate over it directly in the for loop. Within each iteration, the line.strip() method is used to remove any leading or trailing whitespace characters before printing the line.
By utilizing this method, you can achieve the desired outcome of reading and printing each line of the file in a more concise and efficient manner.

Related questions

0 votes
1 answer
asked Jul 2, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Jul 18, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Jul 9, 2019 in Java by Aditya98 (1.3k points)
0 votes
1 answer
asked Jul 3, 2019 in Python by Sammy (47.6k points)

Browse Categories

...