Tiny
Tiny

Reputation: 419

Why doesn't this simple search work?

am simply iterating through an external file (which contains a phrase) and want to see if a line exists (which has the word 'Dad' in it) If i find it, I want to replace it with 'Mum'. Here is the program i've built... but am not sure why it isn't working?!

message_file = open('test.txt','w')
message_file.write('Where\n')
message_file.write('is\n')
message_file.write('Dad\n')
message_file.close()

message_temp_file = open('testTEMP.txt','w')
message_file = open('test.txt','r')

for line in message_file:
    if line == 'Dad':  # look for the word
        message_temp_file.write('Mum')  # replace it with mum in temp file
    else:
        message_temp_file.write(line)  # else, just write the word

message_file.close()
message_temp_file.close()

import os
os.remove('test.txt')
os.rename('testTEMP.txt','test.txt')

This should be so simple...it's annoyed me! Thanks.

Upvotes: 2

Views: 75

Answers (3)

MaSp
MaSp

Reputation: 291

print(message_file.read())

here you already read the whole file. Nothing is left for the for loop to check

A file object always remembers where it stopped to read/write the last time you accessed it. So if you call print(message_file.readline()), the first line of the file is read and printed. Next time you call the same command, the second line is read and printed and so on until you reach the end of the file. By using print(message_file.read()) you have read the whole file and any further call of read or readline will give you nothing

You can get the current position by message_file.tell() and set it to a certain value by message_file.seek(value), or simply reopen the file

Upvotes: 3

Adam Smith
Adam Smith

Reputation: 54163

You don't have any lines that are "Dad". You have a line that is "Dad\n", but no "Dad". In addition, since you've done message_file.read(), the cursor is at the end of your file so for line in message_file will return StopIteration immediately. You should do message_file.seek(0) just before your for loop.

print(message_file.read())
message_file.seek(0)
for line in message_file:
    if line.strip() == "Dad":
        ...

That should put the cursor back at the beginning of the file, and strip out the newline and get you what you need.

Note that this exercise is a great example of how not to do things in general! The better implementation would have been:

in_ = message_file.read()
out = in_.replace("Dad","Mum")
message_temp_file.write(out)

Upvotes: 5

mdadm
mdadm

Reputation: 1363

The problem most likely is due to the fact that your conditional will only match the string "Dad", when the string is actually "Dad\n". You could either update your conditional to:

if line == "Dad\n":

OR

if "Dad" in line:

Lastly, you also read the entire file when you call print(message_file.read()). You either need to remove that line, or you need to put a call to message_file.seek(0) in order for the loop that follows to actually do anything.

Upvotes: 2

Related Questions