0

I want to search for a line in a file and then substitute that line for another. I've found this question: Search and replace a line in a file in Python
In the main question the user asks for a replace function, that works like this:

f = open(file)
for line in f:
    if line.contains('foo'):
        newline = line.replace('foo', 'bar')
        # how to write this newline back to the file

, which is exactly what i need but i can't seem to get it to work. Can someone maybe explicitly use the replace function given in the answer for the problem above and write the code down? (I don't know what to put for "file_path" in the replace function. Is there some way to just tell python to use the current directory or something like that?)

Sorry if it is a trivial question but I've been strugling with it for quite some time now and i can't get it to work. Thanks in advance!

DeltaChief
  • 184
  • 8

0 Answers0