• 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 ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

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?
 
Sheriff
Posts: 24635
56
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

 
Bartender
Posts: 1210
25
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:
 
Is that a spider in your hair? Here, threaten it with this tiny ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!