• Register
1 vote
186 views

Problem:

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()
test_file.close()

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

Please log in or register to answer this question.

1 Answer

1 vote

Solution:

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.

Thanks

13 9 6
94,240 points

Related questions

0 votes
1 answer 5 views
5 views
valueerror: mixing iteration and read methods would lose data
asked Feb 16 ArifulIslam 7.5k points
0 votes
1 answer 17 views
17 views
Problem: error: (-215:assertion failed) !empty() in function 'cv::cascadeclassifier::detectmultiscale'
asked Feb 19 Lucky Guy 1.4k points
0 votes
1 answer 16 views
16 views
Problem: typeerror: '_io.textiowrapper' object is not subscriptable.
asked Feb 19 Dan phillip 1.9k points
0 votes
1 answer 9 views
9 views
Problem input contains nan, infinity or a value too large for dtype('float64').
asked Feb 11 charles mathews 3.8k points
0 votes
1 answer 741 views
741 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, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 113 views
113 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, 2020 alecxe 7.5k points
0 votes
1 answer 555 views
555 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 856 views
856 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.4K views
1.4K 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
1 vote
1 answer 25 views
25 views
Problem : Recently I have started learning Homebrew. Now if I try to run Homebrews brew doctor command on my Mac OS X 10.9.2 I have started receiving the below listed warning message: Warning: Python is installed at /Library/Frameworks/Python.framework Homebrew only ... /usr/local/bin/python. But still if I rerun the brew doctor command I am still facing the same warning message listed above.
asked Apr 4, 2020 morrisBson 3.2k points