+ 2

How to append to a text file?

I've made a quiz, but the only problem is that in the end, when the program is supposed to append the players score to a txt file and then display all scores in the file, all i get is a blank array. could someone tell what i'm doing wrong? edit: I'm doing this from the python IDE on my pc, but am simply posting the code here for help https://code.sololearn.com/cASa9mly9s59/#py

7th Oct 2017, 7:03 PM
X-1
X-1 - avatar
4 ответов
+ 4
@Kirk Schafer I totally agree with your point on file cursor, having append mode set opens the file, and moves the cursor to EOF, ready to write on it, hence the readlines sees there's nothing more to read. @X-1, may I suggest to use seek function to move the cursor to BOF before the call to readlines() maybe like: scores.seek(0) showScores = scores.readlines() Hth, cmiiw
8th Oct 2017, 2:16 PM
Ipang
+ 6
@X-1, link your code in the question mate, people will more likely to help if they can see your code here rather than having to visit your account to see it :)
7th Oct 2017, 2:26 PM
Ipang
+ 4
This is just a wild guess, but files have a 'cursor' like databases and terminals. In my mind, appending means the cursor starts at the end of the data. At the end of your code, you write a line...then readlines() from the same file handle, but the cursor's still at the end of the last write I think. Agree/disagree?
7th Oct 2017, 4:41 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
First, like Ipang said you should actually "post the code here" (in this question post). Second, for simplicity of debugging by others, you could just post a mimimal subset of your code that gives the same error, and that runs in the code playground. Third, this may not be your current problem, but you should fix the spelling of "python projects". (not "pyhton")
7th Oct 2017, 5:14 PM
Erik