title: Python - Is it recommended to always open file with 'b' mode?
body: <p>So I have this simple python function:</p>
  <pre><code>def ReadFile(FilePath):
      with open(FilePath, 'r') as f:
          FileContent = f.readlines()
      return FileContent
  </code></pre>
  <p>This function is generic and used to open all sort of files. However when the file opened is a binary file, this function does not perform as expected. Changing the <a href="https://docs.python.org/2/library/functions.html#open" rel="nofollow noreferrer">open()</a> call to:</p>
  <pre><code>with open(FilePath, 'rb') as f:
  </code></pre>
  <p>solve the issue for binary files (and seems to keep valid in text files as well)</p>
  <h3>Question:</h3>
  <ol>
  <li>Is it safe and recommended to <strong>always</strong> use <code>rb</code> mode for reading a file?</li>
  <li>If not, what are the cases where it is harmful?</li>
  <li>If not, How do you know which mode to use if you don't know what type of file you're working with?</li>
  </ol>
  <hr />
  <h3>Update</h3>
  <pre><code>FilePath = r'f1.txt'
  
  def ReadFileT(FilePath):
      with open(FilePath, 'r') as f:
          FileContent = f.readlines()
      return FileContent
  
  def ReadFileB(FilePath):
      with open(FilePath, 'rb') as f:
          FileContent = f.readlines()
      return FileContent
  
  
  with open(&quot;Read_r_Write_w&quot;, 'w') as f:
      f.writelines(ReadFileT(FilePath))
  
  with open(&quot;Read_r_Write_wb&quot;, 'wb') as f:
      f.writelines(ReadFileT(FilePath))
  
  with open(&quot;Read_b_Write_w&quot;, 'w') as f:
      f.writelines(ReadFileB(FilePath))
  
  with open(&quot;Read_b_Write_wb&quot;, 'wb') as f:
      f.writelines(ReadFileB(FilePath))
  </code></pre>
  <p>where <code>f1.txt</code> is:</p>
  <pre><code>line1
  
  line3
  </code></pre>
  <p>Files <code>Read_b_Write_wb</code>, <code>Read_r_Write_wb</code> &amp; <code>Read_r_Write_w</code> eqauls to the source <code>f1.txt</code>.</p>
  <p>File <code>Read_b_Write_w</code> is:</p>
  <pre><code>line1
  
  
  
  line3
  </code></pre>