• Register
1 vote


I am getting an interesting error: valueerror: mixing iteration and read methods would lose data while I’m trying to run my script on python. I have no idea about this problem.

I am trying to open a text file in my program and read it. Please check my code snippet below if you can find the error from it.

for i in range(len(test_file):
    print test_file.readline()

print test_file

I think I am doing something wrong with using the readline() function. I am looking forward to having a solution.

Thanks, guys.

12 7 7
15,250 points

1 Answer

1 vote


You can not re-open the test_file before closing it, once you open it. 

test_file = open("output.txt","rw") 

The above line could save your day. Please check this and let me if it’s working!

Never forget to leave a kudos if you get a solution.


13 9 6
94,240 points

Related questions

0 votes
1 answer 320 views
Problem: I have recently started learning Python so I have only fundamental knowledge in Python. Please find below my Transaction class for your reference: class Transaction(object):     def __init__(self, company, num, price, date, is_buy):         self.company = company         ... Python and because of lockdown I am unable to find any help to fix this issue. So I am posting my question here.
asked Aug 10 Raphael Pacheco 4.9k points
0 votes
1 answer 93 views
Problem : I am unable to understand what am I missing here: Sort my_short_names in reverse alphabetic order. The sample output from the given program as below : ['Tody', 'Samy', 'Joey', 'Jany', 'Anny'] Below is my code for your reference : my_short_names = ['Jany', 'Samy', 'Anny', 'Joey', 'Tody'] my_short_names.sort() print(my_short_names)
asked Jan 8 alecxe 7.5k points
0 votes
1 answer 303 views
Problem : I am trying to execute below commands. import numpy as np import cv But I am getting following error on my console: >>> runfile('/Users/isaiahnields/.spyder2/temp.py', wdir='/Users/isaiahnields/.spyder2') RuntimeError: module compiled against API version a but this ... it is not working. I had added cv.py to the python2.7 folder in Spyder-Py2 is there anything else I need to add?
asked Dec 4, 2019 alecxe 7.5k points
0 votes
2 answers 679 views
Problem : On installing Anaconda on Ubantu getting below Python error on miss-typed commands fatal python error: py_initialize: unable to get the locale encoding
asked Oct 21, 2019 peterlaw 6.9k points
0 votes
1 answer 1.2K views
Problem : I am trying to convert my working Python 2.7 code into Python 3 code but I am facing following error post data should be bytes, an iterable of bytes, or a file object. it cannot be of type str.
asked Nov 16, 2019 peterlaw 6.9k points