title: How to redirect the output of print statement of (if-else) to a text file in Python
body: <p>I have this piece of code, and print the output to a txt file. 
  However whenever I open a file, 
  f=open("out.txt",'w') it shows unexpected indent. I guess I am placing the line of code to a wrong position.
  Can anyone help. </p>
  
  <pre><code>if(cp&lt;0):
  
      print("No Common Predecessor")
  elif (posa &lt; posb):
  
      if(posa&lt;0):
          posa=0
      print("Common Predecessor: %s" %n[posa])
  else:
  
      if(posb &lt; 0):
          posb=0
      print("Common Predecessor: %s" %m[posb])
  </code></pre>