使用pandas导出DataFrame至Excel报磁盘空间不足错误求助
排查 FileCreateError: [Errno 28] No space left on device 错误
以下是针对该问题的排查方向和解决方法:
检查系统临时目录空间
xlsxwriter在生成Excel文件时会依赖系统临时目录存储中间缓存文件,这个错误可能是临时目录所在磁盘空间不足导致的,和目标文件所在磁盘的剩余空间无关:- 先查看系统临时目录路径:运行
python -c "import tempfile; print(tempfile.gettempdir())" - 检查该目录所在磁盘的剩余空间
- 若临时目录空间不足,可手动指定一个有充足空间的临时目录:
import tempfile with tempfile.TemporaryDirectory(dir="/path/to/your/available/dir") as tmpdir: with pd.ExcelWriter(output_file_path, engine='xlsxwriter', tempdir=tmpdir) as writer: tph_summary.to_excel(writer, sheet_name='tph_summary', index=False) df.to_excel(writer, sheet_name='df', index=False)
- 先查看系统临时目录路径:运行
确认目标文件路径的实际挂载磁盘
排查output_file_path是否指向了你误判的磁盘分区:- 运行
python -c "import os; print(os.path.abspath(output_file_path))"获取文件绝对路径 - 检查该路径对应磁盘的剩余空间(可能你之前查看的是其他分区的空间)
- 运行
验证目标目录的写入权限
部分场景下,目录写入权限不足会被系统误报为空间不足,可手动测试写入权限:import os test_path = os.path.join(os.path.dirname(output_file_path), "test_write.txt") with open(test_path, "w") as f: f.write("test") os.remove(test_path)如果执行失败,说明需要调整目标目录的写入权限。
更换Excel写入引擎
尝试使用openpyxl引擎替代xlsxwriter,看是否能绕过问题:with pd.ExcelWriter(output_file_path, engine='openpyxl', mode='w') as writer: tph_summary.to_excel(writer, sheet_name='tph_summary', index=False) df.to_excel(writer, sheet_name='df', index=False)注意:使用前需先安装依赖
pip install openpyxl
内容的提问来源于stack exchange,提问作者Fachry Dzaky
相关产品推荐
相关产品推荐

