read()的文件大小限制? [英] File size limit for read()?

查看:528
本文介绍了read()的文件大小限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试使用Python 3.5加载大文件时遇到问题。使用 read()不带任何参数有时会给出 OSError:无效参数。然后我试着只读取部分文件,它似乎工作正常。我已经确定它开始在 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(最近的最后一次调用):
文件stdin>, 1,在< module>
OSError:[Errno 22]无效参数
>>> read(int(2.1 * 10 ** 9))
>>> x = open('/ Users / username / Desktop / large.txt','r' read(int(2.2 * 10 ** 9))
Traceback(最近的最后一次调用):
在< module>中的文件< stdin>,第1行
OSError:[Errno 22]无效参数

我也注意到这不会发生Python 2.7。这里是在Python 2.7中运行的相同代码:

 >>> sys.version 
'2.7.10(默认,2015年8月22日,20:33:39)\ n [GCC 4.2.1 Compatible Apple LLVM 7.0.0(clang-700.0.59.1)]'
>>> read(int(2.1 * 10 ** 9))
>>> x = open('/ Users / username / Desktop / large.txt','r' (int(2.2 * 10 ** 9))
>>> x = open('/ Users / username / Desktop / large.txt','r' x = open('/ Users / username / Desktop / large.txt','r')。read()
>>>

我使用OS X El Capitan 10.11.1。

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

解决方案

是的,一个bug。

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

Bad News 它仍然处于打开状态(并且当前处于非活动状态),所以您必须等到解决问题后再开启。无论如何,如果您对这些细节感兴趣,您仍然可以在这里进行讨论。



无论如何,我敢肯定,您可以问题,直到它被修复,通过在处理过程中读取块和链接块,并在写时做同样的事情。笨拙的,但它可能会诀窍。

I'm running into a problem while trying to load large files using Python 3.5. Using read() with no arguments sometimes gave an OSError: Invalid argument. I then tried reading only part of the file and it seemed to work fine. I've determined that it starts to fail somewhere around 2.2GB, below is the example code:

>>> 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

I also noticed that this does not happen in Python 2.7. Here is the same code run in 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()
>>>

I am using OS X El Capitan 10.11.1.

Is this a bug or should use another method for reading the files?

解决方案

Yes, you have bumped into a bug.

Good news is that someone else has also found it and already created an issue for it in the Python bug tracker, see: Issue24658 - open().write() fails on 2 GB+ data (OS X). This, seems, is platform depended (OS-X only) and is reproducible when using read and/or write. Apparently an issue exists with the way fread.c is implemented in the libc implementation for OS-X see here.

Bad News is that it is still open (and, currently, inactive) so, you'll have to wait until it is resolved. Either way, you can still take a look at the discussion there if you're interested for the specifics.

Regardless, I'm pretty sure you can side-step the issue, until it is fixed, by reading in chunks and chaining the chunks during processing and doing the same when writing. Clunky but, it might do the trick.

这篇关于read()的文件大小限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆