Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

read / write to text file - doesn't work all the time?

Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Here is the error

Any ideas?
Posts: 25594
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Patrick De
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Here is the full code

Posts: 1210
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
Space seems cool in the movies, but once you get out there, it is super boring. Now for a fascinating tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
    Bookmark Topic Watch Topic
  • New Topic