【问题标题】:How to write on top of pandas HDF5 'read-only mode' files?如何在 pandas HDF5“只读模式”文件之上写入?
【发布时间】:2025-12-17 03:50:01
【问题描述】:

我正在使用 pandas 内置的 HDF5 方法存储数据。

不知何故,这些 HDF5 文件变成了“只读”文件,当我以写入模式打开这些文件时,我收到很多 Opening xxx in read-only mode 消息,我无法写入它们,这是我真正需要做的事情。

到目前为止,我真的不明白这些文件是如何变成只读的,因为我不知道我编写的一段代码可能会导致这种行为。 (我尝试检查存储在 HDF5 中的数据是否损坏,但我能够读取和操作它,所以它似乎工作正常)

我有 2 个问题:

  1. 如何将数据附加到那些“只读模式”的 HDF5 文件中? (我可以将它们转换回写入模式或任何其他巧妙的解决方案吗?)
  2. 是否有任何 pandas 方法可以将 HDF5 文件默认更改为“只读模式”,这样我就可以避免将这些文件变为只读?

代码:

引发此问题的一段代码是我用来保存我生成的输出的一段代码:

    with pd.HDFStore('data/observer/' + self._currency + '_' + str(ts)) as hdf:

        hdf.append(key='observers', value=df, format='table', data_columns=True)

我还使用这段代码来操作之前生成的输出:

    for the_file in list_dir:
        if currency in the_file:
            temp_df = pd.read_hdf(folder + the_file)
            ...

我还使用一些选择命令从数据文件中获取特定列:

    with pd.HDFStore('data/observer/' + self.currency + '_' + timestamp) as hdf:
        df = hdf.select(key='observers', columns=[x, y])

错误回溯:

File ".../data_processing/observer_data.py", line 52, in save_obs_to_pandas
hdf.append(key='observers', value=df, format='table', data_columns=True)
File ".../venv/lib/python3.5/site-packages/pandas/io/pytables.py", line 963, in append
**kwargs)
File ".../venv/lib/python3.5/site-packages/pandas/io/pytables.py", line 1341, in _write_to_group
s.write(obj=value, append=append, complib=complib, **kwargs)
File ".../venv/lib/python3.5/site-packages/pandas/io/pytables.py", line 3930, in write
self.set_info()
File ".../venv/lib/python3.5/site-packages/pandas/io/pytables.py", line 3163, in set_info
self.attrs.info = self.info
File ".../venv/lib/python3.5/site-packages/tables/attributeset.py", line 464, in __setattr__
nodefile._check_writable()
File ".../venv/lib/python3.5/site-packages/tables/file.py", line 2119, in _check_writable
raise FileModeError("the file is not writable")
tables.exceptions.FileModeError: the file is not writable
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File ".../general_manager.py", line 144, in <module>
gm.run()
File ".../general_manager.py", line 114, in run
list_of_observer_managers = self.load_all_observer_managers()
File ".../general_manager.py", line 64, in load_all_observer_managers
observer = currency_pool.map(self.load_observer_manager, list_of_currencies)
File "/usr/lib/python3.5/multiprocessing/pool.py", line 260, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "/usr/lib/python3.5/multiprocessing/pool.py", line 608, in get
raise self._value
tables.exceptions.FileModeError: the file is not writable

【问题讨论】:

  • (我确实使用 HDF5,但使用的是 h5py/numpy)。你有一些代码的小例子吗?原则上我认为 HDF5 格式不支持将文件设置为只读,但您可以选择以只读模式打开它。参见例如documentation。但实际上,我们可以从您的代码中了解更多信息。
  • 可能的答案取决于您使用 HDF5 文件的方式。请将您的代码添加到问题中
  • 能否也添加错误回溯?
  • @MaxU 我已经查看了我的代码,并发布了我认为我对 HDF5 文件执行的不同类型的操作。它实际上是相当多的代码,所以可能会丢失一些东西。如果我发现新的东西,我会更新你。感谢您的支持:)
  • @MaxU,我搞砸了文件权限,因此出现了问题。很抱歉给您带来麻烦。

标签: python python-3.x pandas hdf5


【解决方案1】:

手头的问题是我搞砸了操作系统文件权限。我试图读取的文件属于root(因为我已经运行了生成这些文件的代码)并且我试图使用user 帐户访问它们。

我正在运行 debian,以下命令(root)解决了我的问题:

chown -R user.user folder

此命令递归地将文件夹内所有文件的权限更改为user.user

【讨论】: