OSError: [Errno 22] Invalid argument for read () in Python

I use Python to read a large file on macOS 10.13. It seems that every time I try to read a large file, it just fails. The code:

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

x = f.read()

print(x)

The scripts work well when the file is file_namesmall, but does not work when reading large files (in my case 3.5 GB):

/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 16 GB of memory, and I'm sure this is not due to memory. And sys.versiongives me the following:

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 doesn't matter if I use 'rb' or 'r', I have the same problem. Here is some debugging information after the first line is executed. The method seems to be opengood:

enter image description here

, python 2.7.10, . , - Python3 Mac. Python 3.5.2 Python 3.6.1 .

, ?

+2

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


All Articles