• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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: 27450
88
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:
 
For my next trick, I'll need the help of a tiny ad ...
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic