0

I want to replace some of the contents of my file. I have this code:

username= input("enter your new username")
password= input("enter the new password")

file = open("testfile.txt", "r")
replaced_content = ""
for line in file:
    line = line.strip()
    new_line = line.replace("username:", "username: " + username)
    replaced_content = replaced_content + new_line + "\n"

file.close()
write_file = open("testfile.txt", "w")
write_file.write(replaced_content)
write_file.close()

Here, testfile.txt contains:

username:
password:

The problem is when I input the replacement text, it's being added rather than replaced. For example, when I enter a username, I want to replace the line "username:" by "username: admin"; but when I run the code repeatedly, it gets added repeatedly, thus:

username: admin admin
password:

If my username is already in the text file I want to replace it with an other one and not adding the new to the other. How can I make this work? (I try to not import packages or other things like that in my code.)

vvvvv
  • 25,404
  • 19
  • 49
  • 81
  • "I try to not import packages or other thing like that in my script" Why? The standard library exists for a reason. – Karl Knechtel Aug 07 '21 at 16:13
  • "The problem is when I enter what I want to replace with, it's adding it not replacing." Well, yes, that's what your code says to do. `line.replace("username:", "username: " + username)`. It looks for the `"username:"` text and then creates *the result of adding the name to that* and does the replacement. Of course it will put the `"username:"` back in. It seems that the `"username:"` text *isn't actually what you want to replace, but instead *the rest of the line after it*, right? It is only possible to solve programming problems when you *clearly understand what you want to do*. – Karl Knechtel Aug 07 '21 at 16:15
  • I edited the question to ask the actual question more clearly, fix some minor grammatical issues, and remove off-topic commentary. Stack Overflow is not a discussion forum and thus [we just want the question, not any social niceties](https://meta.stackoverflow.com/questions/260776/should-i-remove-fluff-when-editing-questions). – Karl Knechtel Aug 07 '21 at 16:22
  • Does this answer your question? [How to search and replace text in a file?](https://stackoverflow.com/questions/17140886/how-to-search-and-replace-text-in-a-file) – pu239 Aug 07 '21 at 16:26

3 Answers3

0

Check if the line equal "username:" and only do the replacement then. In this code it will replace the username: in a line "username: admin" with "username: " + username giving you the extra admin at the end

Andrew
  • 146
  • 5
  • By the way check out the fileinput module that comes with python which is a handy way of processing files and can do in place filtering – Andrew Aug 07 '21 at 16:05
0

The issue is that you find a "username:" in the line and replace it with "username: " + username. So if you had a line like "username: admin", it would simply replace the username as asked, and it would become "username: admin admin".

Try changing the

new_line = line.replace("username:", "username: " + username)

to

new_line = "username: " + username if line.count("username:") > 0 else line
0

Try this (untested, please report of any errors found)v

username= input("enter your new username")
password= input("enter the new password")
new_l=[username, password]

write_file = open("testfile.txt", "r+")
lines=write_file.readlines()

for i,j in zip(lines, new_l):
        write_file.write(i.strip('\n')+j)
        write_file.write('\n')
write_file.close()