Jupy上Python的读/写访问问题

2024-05-11 18:25:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在远程使用Jupyter笔记本服务器,创建文件时使用:

file = open("test.txt","w") file.write("test") file.close()

一切正常,文件测试.txt写入工作目录。当我试图使用Pandas to hu hfs命令时,我的问题出现了:

^{pr2}$

我得到以下错误:

Opening raw_data.h5 in read-only mode
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/pandas/io/pytables.py in open(self, mode, **kwargs)
    586         try:
--> 587             self._handle = tables.open_file(self._path, self._mode, **kwargs)
    588         except (IOError) as e:  # pragma: no cover

/opt/conda/lib/python3.6/site-packages/tables/file.py in open_file(filename, mode, title, root_uep, filters, **kwargs)
    319     # Finally, create the File instance, and return it
--> 320     return File(filename, mode, title, root_uep, filters, **kwargs)
    321 

/opt/conda/lib/python3.6/site-packages/tables/file.py in __init__(self, filename, mode, title, root_uep, filters, **kwargs)
    783         # Now, it is time to initialize the File extension
--> 784         self._g_new(filename, mode, **params)
    785 

tables/hdf5extension.pyx in tables.hdf5extension.File._g_new()

/opt/conda/lib/python3.6/site-packages/tables/utils.py in check_file_access(filename, mode)
    178                 raise IOError("directory ``%s`` exists but it can not be "
--> 179                               "written" % (parentname,))
    180     elif mode == 'a':

OSError: directory ``.`` exists but it can not be written

During handling of the above exception, another exception occurred:

OSError                                   Traceback (most recent call last)
<ipython-input-182-479f2e98ea81> in <module>()
----> 1 pre_clean_data.to_hdf('raw_data.h5','raw_data_santodomingo',mode='w',format='f',data_columns=True)

/opt/conda/lib/python3.6/site-packages/pandas/core/generic.py in to_hdf(self, path_or_buf, key, **kwargs)
   1136 
   1137         from pandas.io import pytables
-> 1138         return pytables.to_hdf(path_or_buf, key, self, **kwargs)
   1139 
   1140     def to_msgpack(self, path_or_buf=None, encoding='utf-8', **kwargs):

/opt/conda/lib/python3.6/site-packages/pandas/io/pytables.py in to_hdf(path_or_buf, key, value, mode, complevel, complib, append, **kwargs)
    267     if isinstance(path_or_buf, string_types):
    268         with HDFStore(path_or_buf, mode=mode, complevel=complevel,
--> 269                       complib=complib) as store:
    270             f(store)
    271     else:

/opt/conda/lib/python3.6/site-packages/pandas/io/pytables.py in __init__(self, path, mode, complevel, complib, fletcher32, **kwargs)
    446         self._fletcher32 = fletcher32
    447         self._filters = None
--> 448         self.open(mode=mode, **kwargs)
    449 
    450     @property

/opt/conda/lib/python3.6/site-packages/pandas/io/pytables.py in open(self, mode, **kwargs)
    589             if 'can not be written' in str(e):
    590                 print('Opening %s in read-only mode' % self._path)
--> 591                 self._handle = tables.open_file(self._path, 'r', **kwargs)
    592             else:
    593                 raise

/opt/conda/lib/python3.6/site-packages/tables/file.py in open_file(filename, mode, title, root_uep, filters, **kwargs)
    318 
    319     # Finally, create the File instance, and return it
--> 320     return File(filename, mode, title, root_uep, filters, **kwargs)
    321 
    322 

/opt/conda/lib/python3.6/site-packages/tables/file.py in __init__(self, filename, mode, title, root_uep, filters, **kwargs)
    782 
    783         # Now, it is time to initialize the File extension
--> 784         self._g_new(filename, mode, **params)
    785 
    786         # Check filters and set PyTables format version for new files.

tables/hdf5extension.pyx in tables.hdf5extension.File._g_new()

/opt/conda/lib/python3.6/site-packages/tables/utils.py in check_file_access(filename, mode)
    154         # The file should be readable.
    155         if not os.access(filename, os.F_OK):
--> 156             raise IOError("``%s`` does not exist" % (filename,))
    157         if not os.path.isfile(filename):
    158             raise IOError("``%s`` is not a regular file" % (filename,))

OSError: ``raw_data.h5`` does not exist

这几句话似乎很中肯,让我觉得写许可才是问题所在:

/opt/conda/lib/python3.6/site-packages/tables/utils.py in check_file_access(filename, mode)
    154         # The file should be readable.
    155         if not os.access(filename, os.F_OK):
--> 156             raise IOError("``%s`` does not exist" % (filename,))
    157         if not os.path.isfile(filename):
    158             raise IOError("``%s`` is not a regular file" % (filename,))

以及

/opt/conda/lib/python3.6/site-packages/tables/utils.py in check_file_access(filename, mode)
    154         # The file should be readable.
    155         if not os.access(filename, os.F_OK):
--> 156             raise IOError("``%s`` does not exist" % (filename,))
    157         if not os.path.isfile(filename):
    158             raise IOError("``%s`` is not a regular file" % (filename,))

OSError: ``raw_data.h5`` does not exist

但是这会让我感到困惑,因为我可以在上面提到的工作目录中编写文本文件。所有和任何帮助的尝试都值得赞赏。在

编辑:如果我使用完整路径:/home/joyvan/work/raw_data.h5'我会得到不同的错误读数。在

data.to_hdf('/home/joyvan/work/raw_data.h5','raw_data_santodomingo',mode='w',format='f',data_columns=True)

生产

OSError                                   Traceback (most recent call last)
<ipython-input-185-de493145e6a7> in <module>()
----> 1 pre_clean_data.to_hdf('/home/joyvan/work/raw_data.h5','raw_data_santodomingo',mode='w',format='f',data_columns=True)

/opt/conda/lib/python3.6/site-packages/pandas/core/generic.py in to_hdf(self, path_or_buf, key, **kwargs)
   1136 
   1137         from pandas.io import pytables
-> 1138         return pytables.to_hdf(path_or_buf, key, self, **kwargs)
   1139 
   1140     def to_msgpack(self, path_or_buf=None, encoding='utf-8', **kwargs):

/opt/conda/lib/python3.6/site-packages/pandas/io/pytables.py in to_hdf(path_or_buf, key, value, mode, complevel, complib, append, **kwargs)
    267     if isinstance(path_or_buf, string_types):
    268         with HDFStore(path_or_buf, mode=mode, complevel=complevel,
--> 269                       complib=complib) as store:
    270             f(store)
    271     else:

/opt/conda/lib/python3.6/site-packages/pandas/io/pytables.py in __init__(self, path, mode, complevel, complib, fletcher32, **kwargs)
    446         self._fletcher32 = fletcher32
    447         self._filters = None
--> 448         self.open(mode=mode, **kwargs)
    449 
    450     @property

/opt/conda/lib/python3.6/site-packages/pandas/io/pytables.py in open(self, mode, **kwargs)
    585 
    586         try:
--> 587             self._handle = tables.open_file(self._path, self._mode, **kwargs)
    588         except (IOError) as e:  # pragma: no cover
    589             if 'can not be written' in str(e):

/opt/conda/lib/python3.6/site-packages/tables/file.py in open_file(filename, mode, title, root_uep, filters, **kwargs)
    318 
    319     # Finally, create the File instance, and return it
--> 320     return File(filename, mode, title, root_uep, filters, **kwargs)
    321 
    322 

/opt/conda/lib/python3.6/site-packages/tables/file.py in __init__(self, filename, mode, title, root_uep, filters, **kwargs)
    782 
    783         # Now, it is time to initialize the File extension
--> 784         self._g_new(filename, mode, **params)
    785 
    786         # Check filters and set PyTables format version for new files.

tables/hdf5extension.pyx in tables.hdf5extension.File._g_new()

/opt/conda/lib/python3.6/site-packages/tables/utils.py in check_file_access(filename, mode)
    172                 parentname = '.'
    173             if not os.access(parentname, os.F_OK):
--> 174                 raise IOError("``%s`` does not exist" % (parentname,))
    175             if not os.path.isdir(parentname):
    176                 raise IOError("``%s`` is not a directory" % (parentname,))

OSError: ``/home/joyvan/work`` does not exist

Tags: pathinpyselftablesmodelibpackages
2条回答
  • 我也遇到了类似的问题。在
  • 结果表明,作为当前用户,我正在运行的文件没有足够的写入权限。在
  • 我用root用户运行了相同的脚本,它起作用了。在

注意:这很晚了,不是对OP的问题的回答,而是对我来说类似的情况,以及对我有用的解决方案。在

相关问题 更多 >