title: replace line if found or append - python
body: <p>I have text that is key-value pairs separated by '='. I would like to replace the line if the key matches. if not, i would like to append it at the bottom. I've tried several ways, including:</p>
  
  <pre><code>def split_command_key_and_value(command):
     if '=' in command:
        command2 = command.split('=')
        return command2
  
  def test(command, path):
     command2 = split_command_key_and_value(command)
     pattern = command2[0]
     myfile = open(path,'r')  # open file handle for read
  # use r'', you don't need to replace '\' with '/'
     result = open(path, 'w')  # open file handle for write
     for line in myfile:
        line = line.strip()  # it's always a good behave to strip what you read from files
        if pattern in line:
           line = command  # if match, replace line
        result.write(line)  # write every line
     myfile.close()  # don't forget to close file handle
     result.close()
  </code></pre>
  
  <p>I know the above is just to replace text, but it deletes the text in the file, and I can't see why. Could someone point me in the right direction?</p>
  
  <p>Thanks</p>
  
  <p>Update:</p>
  
  <p>I'm almost there, but some of my lines have similar keys, so mutiple lines are matching when only 1 should. I've tried to incorporate a regex boundary in my loop with no luck. My code is below. Does anyone have a suggestion?</p>
  
  <p>There is some text in the file that isn't key-value, so I would like to skip that.</p>
  
  <pre><code>   def modify(self, name, value):
        comb = name + ' ' + '=' + ' ' + value + '\n'
        with open('/file/', 'w') as tmpstream:
           with open('/file/', 'r') as stream:
              for line in stream:
                 if setting_name in line:
                    tmpstream.write(comb)
                 else:
                    tmpstream.write(line)
  </code></pre>
  
  <p>I think I got it. See code below.</p>
  
  <pre><code>   def modify(self, name, value):
        comb = name + ' ' + '=' + ' ' + value + '\n'
        mylist = []
        with open('/file/', 'w') as tmpstream:
           with open('/file/', 'r') as stream:
              for line in stream:
                 a = line.split()
                 b = re.compile('\\b'+name+'\\b')
                 if len(a) &gt; 0:
                    if b.search(a[0]):
                       tmpstream.write(comb)
                    else:
                       tmpstream.write(line)
  </code></pre>
  
  <p>I spoke too soon. It stops at the key-value I provide. So, it only writes one line, and doesn't write the lines that don't match.</p>
  
  <pre><code>def modify(name, value):
     comb = name + ' ' + '=' + ' ' + value + '\n'
     mylist = []
     with open('/file1', 'w') as tmpstream:
        with open('/file2', 'r') as stream:
           for line in stream:
              a = line.split()
              b = re.compile('\\b'+name+'\\b')
              if len(a) &gt; 0:
                 if b.search(a[0]):
                    tmpstream.write(comb)
              else:
                tmpstream.write(line)
  </code></pre>
  
  <p>Can anyone see the issue?</p>