Python: the file does not read the whole file, io.FileIO does - why?

The following code executed in python 2.7.2 on windows reads only part of the base file:

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

while this code works very well:

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

Why is the difference? From my reading of documents, they should be executed the same way.

+6
source share
1 answer

You need to open the file in binary mode, or read() will stop at the first EOF character found. And docx is a ZIP file that is supposed to contain such a character.

Try

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

FileIO reads raw bytestreams , and by default they are binary.

+12
source

Source: https://habr.com/ru/post/907254/


All Articles