3

I am using Python to read a large file in macOS 10.13. It seems that everytime I try to read a large file it just simply fails. Code:

f = open('file_name', 'r')

x = f.read()

print(x)

The scripts work well when the file file_name is small, but fails when reading large files (in my case, 3.5GB):

/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/mengzhao/PycharmProjects/testread/readthisfile.py
Traceback (most recent call last):
  File "/Users/mengzhao/PycharmProjects/testread/readthisfile.py", line 3, in <module>
    x = f.read()
OSError: [Errno 22] Invalid argument

Process finished with exit code 1

I have 16GB memory and I am sure it is not because of the memory. And sys.version gives me this:

3.6.1 (default, Apr  4 2017, 09:40:21) 
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)]

And it does not matter whether I use 'rb' or 'r', I just have the same problem. Here are some debug information after the first line is executed. It seems that the open method is fine:

enter image description here

Besides, I changed to python 2.7.10 and the code works. It seems that the problem has something to do with Python3 on Mac. Both Python 3.5.2 and Python 3.6.1 does not work.

Any ideas on how to fix this?

Zhao
  • 2,043
  • 1
  • 17
  • 36

0 Answers0