read()的文件大小限制?

cal*_*co_ 11 python macos file python-2.7 python-3.x

我在尝试使用Python 3.5加载大文件时遇到了问题.read()没有参数的使用有时会给出一个OSError: Invalid argument.然后我尝试只阅读部分文件,它似乎工作正常.我已经确定它在某处开始失败2.2GB,下面是示例代码:

>>> sys.version
'3.5.1 (v3.5.1:37a07cee5969, Dec  5 2015, 21:12:44) \n[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]'
>>> x = open('/Users/username/Desktop/large.txt', 'r').read()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument
>>> x = open('/Users/username/Desktop/large.txt', 'r').read(int(2.1*10**9))
>>> x = open('/Users/username/Desktop/large.txt', 'r').read(int(2.2*10**9))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument
Run Code Online (Sandbox Code Playgroud)

我也注意到在Python 2.7中不会发生这种情况.这是在Python 2.7中运行的相同代码:

>>> sys.version
'2.7.10 (default, Aug 22 2015, 20:33:39) \n[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)]'
>>> x = open('/Users/username/Desktop/large.txt', 'r').read(int(2.1*10**9))
>>> x = open('/Users/username/Desktop/large.txt', 'r').read(int(2.2*10**9))
>>> x = open('/Users/username/Desktop/large.txt', 'r').read()
>>>
Run Code Online (Sandbox Code Playgroud)

我正在使用OS X El Capitan 10.11.1.

这是一个错误还是应该使用另一种方法来读取文件?

Jim*_*ard 6

是的,你碰到了一个bug.

好消息是其他人也发现了它并且已经在Python bug跟踪器中为它创建了一个问题,请参阅:Issue24658 - open().write()2 GB +数据(OS X)失败.这似乎是平台所依赖的(仅限OS-X),并且在使用read和/或时是可重现的write.显然,fread.c在OS-X的libc实现中实现的方式存在问题,请参见此处.

坏消息它仍处于打开状态(当前处于非活动阶段),所以,您必须等到它被解决.无论哪种方式,如果您对这些细节感兴趣,您仍然可以查看那里的讨论.


作为一个解决方案,我很确定你可以解决这个问题,直到通过读取块并在处理过程中链接块来修复它.写作时也一样.不幸的是,它可能会成功.

  • @ Jean-FrançoisFabre可能是更多的马车:-) (3认同)