Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Is it recommended to always open file with 'b' mode?

So I have this simple python function:

def ReadFile(FilePath):
    with open(FilePath, 'r') as f:
        FileContent = f.readlines()
    return FileContent

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 open() call to:

with open(FilePath, 'rb') as f:

solve the issue for binary files (and seems to keep valid in text files as well)

Question:

  1. Is it safe and recommended to always use rb mode for reading a file?
  2. If not, what are the cases where it is harmful?
  3. If not, How do you know which mode to use if you don't know what type of file you're working with?

Update

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("Read_r_Write_w", 'w') as f:
    f.writelines(ReadFileT(FilePath))

with open("Read_r_Write_wb", 'wb') as f:
    f.writelines(ReadFileT(FilePath))

with open("Read_b_Write_w", 'w') as f:
    f.writelines(ReadFileB(FilePath))

with open("Read_b_Write_wb", 'wb') as f:
    f.writelines(ReadFileB(FilePath))

where f1.txt is:

line1

line3

Files Read_b_Write_wb, Read_r_Write_wb & Read_r_Write_w eqauls to the source f1.txt.

File Read_b_Write_w is:

line1



line3
like image 541
idanshmu Avatar asked Sep 29 '22 16:09

idanshmu


People also ask

Why is it best to open a file with with in Python?

With the “With” statement, you get better syntax and exceptions handling. “The with statement simplifies exception handling by encapsulating common preparation and cleanup tasks.” In addition, it will automatically close the file. The with statement provides a way for ensuring that a clean-up is always used.

What is the correct way of opening a file for reading in Python?

Opening Files in Python Python has a built-in open() function to open a file. This function returns a file object, also called a handle, as it is used to read or modify the file accordingly. We can specify the mode while opening a file.

Why we open file in binary mode?

The open() function opens a file in text format by default. To open a file in binary format, add 'b' to the mode parameter. Hence the "rb" mode opens the file in binary format for reading, while the "wb" mode opens the file in binary format for writing. Unlike text files, binary files are not human-readable.


1 Answers

In the Python 2.7 Tutorial: https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files

On Windows, 'b' appended to the mode opens the file in binary mode, so there are also modes like 'rb', 'wb', and 'r+b'. Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written. This behind-the-scenes modification to file data is fine for ASCII text files, but it’ll corrupt binary data like that in JPEG or EXE files. Be very careful to use binary mode when reading and writing such files. On Unix, it doesn’t hurt to append a 'b' to the mode, so you can use it platform-independently for all binary files.

My takeaway from that is using 'rb' seems to the best practice, and it looks like you ran into the problem they warn about - opening a binary file with 'r' on Windows.

like image 192
robert_x44 Avatar answered Oct 05 '22 07:10

robert_x44