• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 28296
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Can you hear that? That's my theme music. I don't know where it comes from. Check under this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic