简体   繁体   English

Python:文件不读取整个文件,io.FileIO确实 - 为什么?

[英]Python: File doesn't read whole file, io.FileIO does - why?

The following code, executed in python 2.7.2 on windows , only reads in a fraction of the underlying file: 以下代码在Windows上的 python 2.7.2中执行,只读取基础文件的一小部分:

import os

in_file = open(os.path.join(settings.BASEPATH,'CompanyName.docx'))
incontent = in_file.read()
in_file.close()

while this code works just fine: 而这段代码工作得很好:

import io
import os

in_file = io.FileIO(os.path.join(settings.BASEPATH,'CompanyName.docx'))
incontent = in_file.read()
in_file.close()

Why the difference? 为什么不同? From my reading of the docs, they should perform identically. 从我对文档的阅读中,它们的表现应该相同。

You need to open the file in binary mode, or the read() will stop at the first EOF character it finds. 您需要以二进制模式打开文件,否则read()将在找到的第一个EOF字符处停止。 And a docx is a ZIP file which is guaranteed to contain such a character somewhere. docx是一个ZIP文件,保证在某处包含这样的字符。

Try 尝试

in_file = open(os.path.join(settings.BASEPATH,'CompanyName.docx'), "rb")

FileIO reads raw bytestreams and those are "binary" by default. FileIO读取原始字节流 ,默认情况下为“二进制”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM