[英]Is there a way around "OSError: [Errno 22] Invalid argument"?
我正在尝试将变量保存到要调用的文件中。 这通常是用泡菜完成的,所以这就是我开始的。 我取了一小部分数据样本,看看它是否会保存。 该样本达 20 mb。 我的总数据大约大 205 倍(4001 mb)。 此示例已保存,但是当我尝试保存完整数据时,我遇到了OSError: [Errno 22] Invalid argument
经过进一步探索,我发现pickle有一个错误,它不允许您生成更大的4GB文件。 这比我的数据占用的存储量少一点。
在这里陈述: 在pickle.dump中有“OSError:[Errno 22]无效参数”的任何想法?
这里说这是我使用的 OS X 上的一个问题https://bugs.python.org/issue24658
我找到了这段代码,但无法理解
import pickle
class MacOSFile(object):
def __init__(self, f):
self.f = f
def __getattr__(self, item):
return getattr(self.f, item)
def read(self, n):
# print("reading total_bytes=%s" % n, flush=True)
if n >= (1 << 31):
buffer = bytearray(n)
idx = 0
while idx < n:
batch_size = min(n - idx, 1 << 31 - 1)
# print("reading bytes [%s,%s)..." % (idx, idx + batch_size), end="", flush=True)
buffer[idx:idx + batch_size] = self.f.read(batch_size)
# print("done.", flush=True)
idx += batch_size
return buffer
return self.f.read(n)
def write(self, buffer):
n = len(buffer)
print("writing total_bytes=%s..." % n, flush=True)
idx = 0
while idx < n:
batch_size = min(n - idx, 1 << 31 - 1)
print("writing bytes [%s, %s)... " % (idx, idx + batch_size), end="", flush=True)
self.f.write(buffer[idx:idx + batch_size])
print("done.", flush=True)
idx += batch_size
def pickle_dump(obj, file_path):
with open(file_path, "wb") as f:
return pickle.dump(obj, MacOSFile(f), protocol=pickle.HIGHEST_PROTOCOL)
def pickle_load(file_path):
with open(file_path, "rb") as f:
return pickle.load(MacOSFile(f))
在我的代码中
with open("file.pickle", "wb") as f:
pickle.dump((boards, value), f)
我使用了一个简单的转储
我想知道是否有人能够解释上面提供的代码的作用以及它是如何工作的? 来源( https://www.programmersought.com/article/3832726678/ )
重新创建它的一种简单方法是创建一个庞大的列表并保存它。
此代码需要一分钟,但会在您想要读取或写入时保存数据,您需要使用提供的读取和写入功能:
import pickle
class MacOSFile(object):
def __init__(self, f):
self.f = f
def __getattr__(self, item):
return getattr(self.f, item)
def read(self, n):
# print("reading total_bytes=%s" % n, flush=True)
if n >= (1 << 31):
buffer = bytearray(n)
idx = 0
while idx < n:
batch_size = min(n - idx, 1 << 31 - 1)
# print("reading bytes [%s,%s)..." % (idx, idx + batch_size), end="", flush=True)
buffer[idx:idx + batch_size] = self.f.read(batch_size)
# print("done.", flush=True)
idx += batch_size
return buffer
return self.f.read(n)
def write(self, buffer):
n = len(buffer)
print("writing total_bytes=%s..." % n, flush=True)
idx = 0
while idx < n:
batch_size = min(n - idx, 1 << 31 - 1)
print("writing bytes [%s, %s)... " % (idx, idx + batch_size), end="", flush=True)
self.f.write(buffer[idx:idx + batch_size])
print("done.", flush=True)
idx += batch_size
def pickle_dump(obj, file_path):
with open(file_path, "wb") as f:
return pickle.dump(obj, MacOSFile(f), protocol=pickle.HIGHEST_PROTOCOL)
def pickle_load(file_path):
with open(file_path, "rb") as f:
return pickle.load(MacOSFile(f))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.