0

I've seen really complex answers on this website as how to edit a specific line on a file but I was wondering if there was a simple way to do it? I want to search for a name in a file, and on the line that I find that name on, I want to add an integer to the end of the line (as it is a score for a quiz). Or could you tell me how I can replace the entirety of the line with new data? I have tried a lot of coding but either no change is made, or all of the data in the file gets deleted. I tried this....

with open ('File.py', 'r') as class_file:
        for number, line in enumerate(class_file):
            if name in line:
                s=open('File.py', 'r').readlines()
                s[number]=str(data)
                class_file=open('File.py', 'w')
                class_file.writelines(new_score)
                class_file.close()

As well as this function....

def replace (file, line_number, add_score):
    s=open(file, 'w')
    new_data=line[line_number].replace(line, add_score)
    s.write(str(new_data))
    s.close()

As well as this...

def replace_score(file_name, line_num, text):
    new = open(file_name, 'r').readlines()
    new[line_num] = text
    adding_score= open(file_name, 'w')
    adding_score.writelines(new)
    adding_score.close()

But I still can't get it to work. The last code works if I'm trying to replace the first line, but not the others.

1
  • I think I've accepted it now :) @Tim Osadchiy Commented Mar 1, 2015 at 22:09

2 Answers 2

1

You need to get the content of the file. Close the file. Modify the content and rewrite the file with the modified content. Try the following:

def replace_score(file_name, line_num, text):
  f = open(file_name, 'r')
  contents = f.readlines()
  f.close()

  contents[line_num] = text+"\n"

  f = open(file_name, "w")
  contents = "".join(contents)
  f.write(contents)
  f.close()

replace_score("file_path", 10, "replacing_text")
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much, but for some reason it isn't working. Is the updated data meant to be sent to the 'test_data' file because that remains blank and then in the original file that I was trying to change, the data still just gets added on to the end of the file. Also, what does the "".join do? @Tim Osadchiy
I am so sorry. I've corrected my answer. Updated answer is sent to the specified file_name. "".join(contents) joins the modified list of lines from the file into one string (line).
Wow I feel so bad because I keep asking you, but again it just adds the score to the end of the file instead of replacing it. Is this just my python not working? This code looks as though it should work @Tim Osadchiy
Function replace_score replaces the line number line_num in the file_name with a replacing text. You need to call it and provide the number of the line with the score. So lets say you have 3 lines in your file. Third one is score. Then you need to call replace_score("file_name", 2, "score is 78")
Thank you! It does work I discovered that there was a problem earlier on in my code! Sorry @Tom Osadchiy
1

This is Tim Osadchiy's code:

def replace_score(file_name, line_num, text):
     f = open(file_name, 'r')
     contents = f.readlines()
     f.close()

     contents[line_num] = text+"\n"

     f = open(file_name, "w")
     contents = "".join(contents)
     f.write(contents)
     f.close()

replace_score("file_path", 10, "replacing_text")

This code does work but just remember that the line_num will always be one above the actual line number (as it is an index). So if you wanted line 9 then enter 8, not 9. Also, do not forget to put .txt at the end of the file path (I would've commented but do not have a high enough reputation)

1 Comment

Thankyou. I realised that I made a mistake earlier on in my program which prevented that from working. @Zeldor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.