使用Jupyter Notebook保存文件时持续出现[Errno 9]错误求助
解决Jupyter Notebook保存时的[Errno 9] Bad file descriptor错误
问题现象
在Firefox浏览器中使用Jupyter Notebook,每次运行程序都会触发保存错误:[E 19:06:49.747 NotebookApp] Error while saving file: xG_SetPiece_Model.ipynb [Errno 9] Bad file descriptor
PowerShell中的完整报错信息如下:
[E 19:06:49.747 NotebookApp] Error while saving file: xG_SetPiece_Model.ipynb [Errno 9] Bad file descriptor Traceback (most recent call last): File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\site-packages\notebook\services\contents\filemanager.py", line 493, in save self._save_notebook(os_path, nb) File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\site-packages\notebook\services\contents\fileio.py", line 290, in _save_notebook nbformat.write(nb, f, version=nbformat.NO_CONVERT) File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\contextlib.py", line 126, in __exit__ next(self.gen) File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\site-packages\notebook\services\contents\fileio.py", line 210, in atomic_writing yield f File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\contextlib.py", line 126, in __exit__ next(self.gen) File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\site-packages\notebook\services\contents\fileio.py", line 118, in atomic_writing os.fsync(fileobj.fileno()) OSError: [Errno 9] Bad file descriptor [W 19:06:49.747 NotebookApp] 500 PUT /api/contents/xG_SetPiece_Model.ipynb (127.0.0.1): Unexpected error while saving file: xG_SetPiece_Model.ipynb [Errno 9] Bad file descriptor [W 19:06:49.747 NotebookApp] Unexpected error while saving file: xG_SetPiece_Model.ipynb [Errno 9] Bad file descriptor [E 19:06:49.747 NotebookApp] { "Host": "localhost:8888", "Accept": "application/json, text/javascript, */*; q=0.01", "Referer": "http://localhost:8888/notebooks/xG_SetPiece_Model.ipynb", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0" } [E 19:06:49.762 NotebookApp] 500 PUT /api/contents/xG_SetPiece_Model.ipynb (127.0.0.1) 1319.730000ms referer=http://localhost:8888/notebooks/xG_SetPiece_Model.ipynb [I 19:07:33.833 NotebookApp] Saving file at /xG_SetPiece_Model.ipynb [I 19:09:48.442 NotebookApp] Saving file at /xG_SetPiece_Model.ipynb [E 19:09:51.971 NotebookApp] Error while saving file: xG_SetPiece_Model.ipynb [Errno 9] Bad file descriptor Traceback (most recent call last): File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\site-packages\notebook\services\contents\filemanager.py", line 493, in save self._save_notebook(os_path, nb) File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\site-packages\notebook\services\contents\fileio.py", line 290, in _save_notebook nbformat.write(nb, f, version=nbformat.NO_CONVERT) File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\contextlib.py", line 126, in __exit__ next(self.gen) File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\site-packages\notebook\services\contents\fileio.py", line 210, in atomic_writing yield f File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\contextlib.py", line 126, in __exit__ next(self.gen) File "C:\Users\hp\AppData\Local\Programs\Python\Python39\lib\site-packages\notebook\services\contents\fileio.py", line 118, in atomic_writing os.fsync(fileobj.fileno()) OSError: [Errno 9] Bad file descriptor
系统环境
- Windows 10
- Python 3.9.12(64位)
- Pip 22.3
- Jupyter 1.0.0
已尝试无效方案
- 重新安装Jupyter Notebook(GitHub建议方案)
- 修复文件权限问题(Stack Overflow建议方案)
可行解决方案
1. 禁用原子写入功能
Jupyter的原子写入机制在Windows部分环境下会触发文件描述符错误,通过配置关闭该功能:
- 打开PowerShell,执行命令生成配置文件:
jupyter notebook --generate-config - 找到生成的配置文件(路径通常为
C:\Users\hp\.jupyter\jupyter_notebook_config.py) - 打开文件,找到
c.FileContentsManager.use_atomic_writing行,取消注释并设置为False:c.FileContentsManager.use_atomic_writing = False - 重启Jupyter Notebook后测试保存功能
2. 更换浏览器测试
部分浏览器的进程或缓存可能干扰文件操作,尝试使用Chrome或Edge打开Notebook,验证是否仍出现保存错误
3. 移动Notebook文件位置
若文件存放在系统盘用户目录,可能存在隐性权限限制,将.ipynb文件移动到D盘等非系统盘的普通目录,再重新打开运行
4. 更新依赖包
依赖版本不兼容可能导致问题,执行以下命令更新相关组件:
pip install --upgrade jupyter notebook nbformat
内容的提问来源于stack exchange,提问作者DataNoob
相关产品推荐
相关产品推荐

