-1

I have a file which has only + and - in 1 line and there are multiple lines. So basically file is as below.

-++++
+----
+++++

and I have a script which does health check of some websites and extending these lines. So, every line above means another website from a list. (e.g: ["example.com", "google.com", "test.com"]

My script runs every 5minutes and everytime it runs I am adding plus or minus depends on website's availability.

I tried somethings like using readlines method but it returns me ['-++++\n', '+----\n', '+++++\n']. I am a bit stuck with overriding same file with new value both because I don't know what's best way and because of these new lines characters in the returned list from readlines method.

What would be your suggestion for solving this issue?

Anisa Kollenhag
  • 103
  • 1
  • 8
  • 1
    Does this answer your question? [Appending characters to each line in a txt file with python](https://stackoverflow.com/questions/47276506/appending-characters-to-each-line-in-a-txt-file-with-python) – Gabe Feb 28 '20 at 20:55
  • Can you share your current code? The newline issue is simple to solve, by the way, there is at least one very popular question on the subject here on SO. – AMC Feb 28 '20 at 20:55

1 Answers1

0

You could use rstrip() on each element in the list and then add a + or -

You then should be able to just set lines in the file equal to elements in the list.

EDIT: something that could be giving you trouble is that in python strings are immutable, meaning any time you make a change you have to "re-set" the variable.

IE

x = "test string\n" x.rstrip() #prints "test string"

x is still equal to "test string\n" though, you need to do this

x = "test string\n" x = x.rstrip() #sets x to "test string"

sntrenter
  • 135
  • 5