stange - linecache.getline puzzle

I don’t get it. What am I doing wrong?

self.firstRun = 0

# Check if log file exist
try:
    self.checkFile = open('rec.txt')
# if it doesn't write it
except IOError:
    self.checkFile = open('rec.txt', 'w')
    self.checkFile.write('File INI\n\nFirst 
Run = TRUE\nTime = '+str(self.getTime)+'\n')
    self.checkFile.close()
# otherwise just read it and print it
else:
    self.checkFile = open('rec.txt', 'r')
    for line in fileinput.input(['rec.txt']):
        print (line)
    self.checkFile.close()

# For appending purposes
try:
    self.recordFile = open('rec.txt')
except IOError:
    print "File rec.txt not found"
else:
    self.recordFile = open('rec.txt', 'r')
    self.readLine = 
linecache.getline('rec.txt', 3)
    if self.readLine == "First Run = TRUE":
        self.firstRun = 1
        print "##"+str(self.readLine)
    for line in fileinput.input(['rec.txt']):
        print (line)
    self.recordFile.close()

The print for self.readLine is ##First Run = TRUE, but strangely (at least for me) print (line) has some extra lines:

File INI

First Run = TRUE

Time = 10.8168613321

Still I don’t understand why self.readLine is not equal to “First Run = TRUE”. I know it doesn’t because when I use this code;

if self.firstRun == 1:
    self.hVoice001 = 
loader.loadSfx("Sound/silent.mp3")
else:
    self.hIV = Sequence(Wait(20), 
                            
Parallel(self.hVoiceID_001, 
                                    

Func(self.narLetter_0.__setitem__, "text", 

"Welcome,"), 
                                    

Func(self.narLetter_0.__setitem__, "text_fg", 

(1,1,0,1))), Wait(8))
    self.hIV.start()

self.hIV plays
However, when I use this:

if self.firstRun == 0:
    self.hVoice001 = 

loader.loadSfx("Sound/silent.mp3")
else:
    self.hIV = Sequence(Wait(20), 
                            

Parallel(self.hVoiceID_001, 
                                    

Func(self.narLetter_0.__setitem__, "text", 

"Welcome,"), 
                                    

Func(self.narLetter_0.__setitem__, "text_fg", 

(1,1,0,1))), Wait(8))
    self.hIV.start()

The sequence doesn’t play.
I don’t see why it’s not working as I think it should.

Getline’s return value also includes end of line delimiter. If you don’t need it, strip it off with .rstrip("\n")
For cross platform consistency, I’d recommend using “rU” mode for reading.

Arrrggggggg

I’m not getting it.
I tried:

"First Run = TRUE\n".rstrip("\n")
"First Run = TRUE".rstrip("\n")
str(self.readLine)+"\n".rstrip("\n")
str(self.readLine).rstrip("\n")

Still the results are the same as before.
I don’t know what else to do.
I spent practically two nights searching the net for info on getting this to work, and nothing I try works. I hadn’t come across .rstrip before, but not ynjh_jo has drawn it to my attention, I still am not getting it to work. I don’t know what I am doing wrong.
As regards ‘rU’ mode which I have never seen in all my searches, I haven’t been able to check it out because my internet connection is so slow that sometime pages don’t even load, and I have to close it and return later. This is quite frustrating.

If I can get .rstrip work, and it can work with a variable like this:

str(self.readLine)+"\n".rstrip("\n")

then I think that would solve a major problem for me and allow me to move on. But right now I’m stuck - stuck in a rut. HELP

Thanks ynjh_jo for your contribution. i’d appreciate any more help you can give.

What you should strip is the result of getline, then save it to readLine :

self.readLine = linecache.getline(***).rstrip("\n")

Why do you have to spend your time browsing the net just to learn python ?
If you’re on Windows, there is already a .chm in your python installation.

Yeeeeeeaaaah! That’s it!
A million thanks to you ynjh_jo.
I would never ever ever had known that was the way to do it.
Thank you! Thank you! Thank you!

And thank you for the pointer to the python .chm. I did not know that.
Now I can move on. Thanks