Python - 是否建议始终使用 'b' 模式打开文件?
Python - Is it recommended to always open file with 'b' mode?
所以我有这个简单的 python 函数:
def ReadFile(FilePath):
with open(FilePath, 'r') as f:
FileContent = f.readlines()
return FileContent
此函数是通用的,用于打开各种文件。但是,当打开的文件是二进制文件时,此函数不会按预期执行。将 open() 调用更改为:
with open(FilePath, 'rb') as f:
解决了二进制文件的问题(似乎在文本文件中也有效)
问题:
- 是否安全并建议始终使用
rb
模式读取文件?
- 如果不是,它在哪些情况下有害?
- 如果不是,如果您不知道正在处理的文件类型,您怎么知道要使用哪种模式?
更新
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))
其中 f1.txt
是:
line1
line3
文件 Read_b_Write_wb
、Read_r_Write_wb
和 Read_r_Write_w
等于来源 f1.txt
。
文件 Read_b_Write_w
是:
line1
line3
在 Python 2.7 教程中:
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.
我从中得出的结论是使用 'rb' 似乎是最佳实践,看起来你 运行 遇到了他们警告的问题 - 打开一个带有 'r' 的二进制文件Windows.
所以我有这个简单的 python 函数:
def ReadFile(FilePath):
with open(FilePath, 'r') as f:
FileContent = f.readlines()
return FileContent
此函数是通用的,用于打开各种文件。但是,当打开的文件是二进制文件时,此函数不会按预期执行。将 open() 调用更改为:
with open(FilePath, 'rb') as f:
解决了二进制文件的问题(似乎在文本文件中也有效)
问题:
- 是否安全并建议始终使用
rb
模式读取文件? - 如果不是,它在哪些情况下有害?
- 如果不是,如果您不知道正在处理的文件类型,您怎么知道要使用哪种模式?
更新
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))
其中 f1.txt
是:
line1
line3
文件 Read_b_Write_wb
、Read_r_Write_wb
和 Read_r_Write_w
等于来源 f1.txt
。
文件 Read_b_Write_w
是:
line1
line3
在 Python 2.7 教程中: 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.
我从中得出的结论是使用 'rb' 似乎是最佳实践,看起来你 运行 遇到了他们警告的问题 - 打开一个带有 'r' 的二进制文件Windows.