0

I want to replace a String in a file, so I made this method in a class

class Utils:
    def replaceJS(self, archive):
        myfile = open(archive, "r+")
        for line in myfile:
            line.replace('https://kozea.github.io/pygal.js/2.0.x/pygal-tooltips.min.js', "pygal-tooltips.min.js")
        myfile.close()

What I expect is to open the archive, read every line and replace https://kozea.github.io/pygal.js/2.0.x/pygal-tooltips.min.js for pygal-tooltips.min.js

When I call the function I dont get any error but the line stills there without changes

Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
  • 3
    Possible duplicate of [How to search and replace text in a file using Python?](https://stackoverflow.com/questions/17140886/how-to-search-and-replace-text-in-a-file-using-python) – Bram Vanroy Oct 24 '18 at 14:23
  • 1
    How did you call the function? `.replace()` does not work in-line. Also I don't see you writing the changes back to file. – Austin Oct 24 '18 at 14:23
  • Have a look at the `fileinput` module (you want to read about _Optional in-place filtering_) – gboffi Oct 24 '18 at 14:41

0 Answers0