如何在pandas HDF5'只读模式'之上书写文件?

时间:2018-01-25 14:50:34

标签: python python-3.x pandas hdf5

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

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

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

我有两个问题:

  1. 如何将数据附加到“只读”模式&#39; HDF5文件? (我可以将它们转换回写入模式或任何其他聪明的解决方案吗?)
  2. 是否有任何pandas方法可以将HDF5文件更改为“只读”模式&#39;默认情况下,我可以避免首先将这些文件转换为只读文件吗?
  3. 代码:

    引发此问题的代码是,我用来保存我生成的输出的部分:

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

    我也使用一些select命令从数据文件中获取特定的列:

        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
    

1 个答案:

答案 0 :(得分:1)

手头的问题是我搞砸了OS文件权限。我试图阅读的文件属于root(因为我运行了使用根生成这些文件的代码),我试图使用user帐户访问它们。

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

chown -R user.user folder

此命令以递归方式将该文件夹中所有文件的权限更改为user.user