title: Python - Replace newline with return carriage/newline in file output
body: <p>First of I've only been writing python for a couple of months</p>
  
  <p>Seconly, I have a txt file that needs to be processed. In order for it to be processed properly each lines has to end with '\r\n'.</p>
  
  <p>However, that is not always tha case, sometimes it onle contains the '\n' (newline).</p>
  
  <p>Therefore, I need to check the file and if the '\r\n' is missing replace '\n' with '\r\n'.</p>
  
  <p>I have tried a couple of ways and failed. </p>
  
  <p>First I tried the following:</p>
  
  <pre><code>with open("initial_file.txt",'rb') as file_content:
    #1253 is a Windows code page used to write modern Greek.
    mycon = file_content.read().decode('cp1253')
  
  if (mycon.count("\r\n") == 0) and (mycon.count("\n") &gt; 0):
    with open("destination_file.txt",'w') as file_replace:
       file_replace.write(mycon.replace("\n", "\r\n").encode('cp1253'))
  </code></pre>
  
  <p>But instead of replacing '\n' with '\r\n' I got '\r\r\n'.</p>
  
  <p>So I tried another approach:</p>
  
  <pre><code>rf = open("initial_file.txt", 'rb')
  wf = open("destination_file.txt",'wb')
  mycon = rf.read().decode('cp1253')
  if (mycon.count('\r\n') == 0) and (mycon.count('\n') &gt; 0):
      for line in rf:
          newline = line.rstrip('\n')
          wf.write(newline).encode('cp1253')
          wf.write('\r\n').encode('cp1253') 
  </code></pre>
  
  <p>It worked the first time and then it didn't.</p>
  
  <p>I am not sure what I'm doing wrong and I could appreciate some help.</p>