Is there a way around "OSError: [Errno 22] Invalid argument"?

Is there a way around "OSError: [Errno 22] Invalid argument"?

我正在尝试将变量保存到文件中以供调用。这通常是用 pickle 完成的,所以这就是我开始的。我取了一小部分数据样本,看看它是否会保存。该样本总计 20 MB。我的总数据大约大 205 倍(4001 MB)。此样本已保存,但当我尝试将完整数据保存时,我 运行 进入 OSError: [Errno 22] Invalid argument

经过进一步探索,我发现 pickle 有一个错误,不允许你生成更大的 4gb 文件。这比我的数据占用的存储量少一点。

此处声明:Any idea with "OSError: [Errno 22] Invalid argument" in pickle.dump?

这里说是我用的OSX上的问题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))