I am not quite sure I understand this. I have code that enters in grades to a text file. Works great. Here is the code :
And then I have a set of code that reads from the text file
This all works great. Python enters the values into the textfile and I can read them using the code.
Then I try this :
-I manually go into the TEXT FILE and add a set of numbers - appending the ones I already have.
-After I do that, I get an error (below).
-Nothing else in my READ of the text file code has changed, just inputting the numbers manually.
If I understand right: the file generated by Code Fragment 1 can be read into Code Fragment 2 successfully, but if you manually edit that file then it can't be read into Code Fragment 2 successfully?
If so then the conclusion must surely be that your edits are the problem. So have a look at the "before" version -- the one which works -- and compare it to the "after" version, which doesn't work. Try to see what's different. If you don't see any difference then post the two versions and let another set of eyes do the looking.
posted 3 years ago
I think I found the solution - although my assumption might not be correct, it seems to work. When the WRITE program finishes, it closes the file with inFile.close().
My guess is when I edit the file using Notepad, it does not close the file properly. I added this :
At the beginning of the program and now it works fine. Might be a poor solution, it might be windows things or a python thing.
The problem with f.readline() is that it retains the newline character in return value.
You get that error if your file has empty lines and f.readline() returns just "\n". int() doesn't know what to do with a "\n".
A more pythonic way of doing this:
Is that a spider in your hair? Here, threaten it with this tiny ad:
professionally read, modify and write PDF files from Java