title: Python - Write content into existing file
body: <p>So my aim is to delete all the \n from the end of every file in a directory, then append the file. Struggling to do the appending part of it.</p>
  
  <pre><code>import os
  print("Copy paste full directory path here:")
  directory = input()
  
  
  for filename in os.listdir(directory):
      if filename.endswith(".txt"):
          with open(os.path.join(directory, filename), "r+") as f:
              lines = f.readlines()        
              for line in lines:
                  if lines[-1] in ['\n', '\r']:
                      lines = lines[:-1]
                      print(lines)     
              f.writelines(lines)
  </code></pre>
  
  <p>So this does practically what I want, however its adds everything underneath the content, rather than replacing it. Could I get help for this please :)?</p>