Skip to main content
 首页 » 编程设计

python之如何在 pandas HDF5 'read-only mode' 文件之上写入

2025年01月19日9三少

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

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

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

我有两个问题:

  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 

请您参考如下方法:

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

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

chown -R user.user folder 

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