idanshmu
idanshmu

Reputation: 5251

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

Upvotes: 5

Views: 2813

Answers (1)

robert_x44
robert_x44

Reputation: 9314

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.

Upvotes: 4

Related Questions