title: How to replace a line in a file with a string in Python, whose length is less than the line to be replaced?
body: <p>I am trying to replace a line in a file, with a string whose length differs based on the input.
  If the length of string is equal to or greater than the line to be replaced,
  then the file is overwritten correctly. Whereas, if the string is shorter than the line to be replaced,
  then after replacing, part of the line will be appended at the end.</p>
  
  <p>The code snippet used for writing to the file is given below.</p>
  
  <pre><code>replace_value = status
  lines = []
  filename = open(os.path.join(dst_path, asilSafety_path),'r+')
  for line in filename.readlines():
      line.strip('\n')
      if line.startswith('export SAFETY_SRV_MODE'):
          bits = line.split('=')
          config, value = bits
          src = config + '=' + value.strip('\n')
          target = config + '= ' + replace_value
          break
  filename.seek(0)
  for line in filename.readlines():
      line = line.replace(src,target)
      lines.append(line)
  filename.seek(0)
  for line in lines:
      filename.write(line)
  filename.close()
  </code></pre>
  
  <p>I am passing the file path and the string to be written as arguments to the function i.e., safety_configuration(dst_path, status). The variable 'replace_value' has the string to be replaced with.</p>
  
  <p>Can someone please tell me, wat am i doing wrong? Or is there any other way to implement this?</p>