You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows下Python程序报OSError: [Errno 28]磁盘空间不足的解决办法

问题描述

运行一个包含网络请求、会下载大量数据的Python程序时,出现如下报错:

Traceback (most recent call last):
  File "C:\Python311\Lib\site-packages\pandas\io\formats\csvs.py", line 261, in save
    self._save()
  File "C:\Python311\Lib\site-packages\pandas\io\formats\csvs.py", line 266, in _save
    self._save_body()
  File "C:\Python311\Lib\site-packages\pandas\io\formats\csvs.py", line 304, in _save_body
    self._save_chunk(start_i, end_i)
  File "C:\Python311\Lib\site-packages\pandas\io\formats\csvs.py", line 315, in _save_chunk
    libwriters.write_csv_rows(
  File "pandas\_libs\writers.pyx", line 72, in pandas._libs.writers.write_csv_rows
OSError: [Errno 28] No space left on device

During handling of the above exception, another exception occurred:

OSError: [Errno 28] No space left on device

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\elena\Desktop\actors.py", line 57, in <module>
    get_popular_movies()
  File "C:\Users\elena\Desktop\actors.py", line 49, in get_popular_movies
    csv_edges.to_csv('edges.csv',index=False,header=True)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\pandas\util\_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\pandas\core\generic.py", line 3720, in to_csv
    return DataFrameRenderer(formatter).to_csv(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\pandas\util\_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\pandas\io\formats\format.py", line 1189, in to_csv
    csv_formatter.save()
  File "C:\Python311\Lib\site-packages\pandas\io\formats\csvs.py", line 241, in save
    with get_handle(
  File "C:\Python311\Lib\site-packages\pandas\io\common.py", line 133, in __exit__
    self.close()
  File "C:\Python311\Lib\site-packages\pandas\io\common.py", line 125, in close
    handle.close()
OSError: [Errno 28] No space left on device

推测是磁盘空间不足导致,请问Windows系统下该如何解决?


解决办法

一、紧急释放磁盘空间

  • 清理临时文件:按下Win + R输入%temp%,打开临时文件夹,全选删除所有文件(部分正在使用的文件无法删除,跳过即可);再输入temp和prefetch,重复清理操作。
  • 启用Windows存储感知:打开「设置」→「系统」→「存储」,开启「存储感知」,它会自动清理临时文件、回收站内容和下载文件夹里的旧文件。
  • 清理回收站:右键回收站图标,选择「清空回收站」。
  • 删除大文件/无用文件:在存储设置的「存储使用情况」里查看各文件夹占用空间,找出视频、压缩包、旧安装包等大文件直接删除;卸载不常用的软件。

二、修改Python程序的输出路径

如果C盘空间不足,可将CSV文件保存到剩余空间充足的磁盘(比如D盘、E盘),修改代码中的路径:

# 原代码
csv_edges.to_csv('edges.csv',index=False,header=True)
# 修改后,指定其他磁盘路径
csv_edges.to_csv('D:/data/edges.csv',index=False,header=True)

三、优化程序减少磁盘占用

  • 分批次写入数据:不要一次性将所有数据存入DataFrame再写入CSV,而是分批次下载、分批次写入文件,避免内存和磁盘被大量数据占用。
  • 压缩存储数据:如果不需要明文查看CSV,可将数据保存为压缩格式(如.zip),pandas支持直接写入压缩文件:
csv_edges.to_csv('edges.zip', index=False, header=True, compression='zip')
  • 清理程序产生的临时文件:在程序中加入清理临时下载文件的逻辑,避免下载的大量原始数据堆积在磁盘上。

四、扩容磁盘空间

如果上述方法无法解决,可考虑:

  • 扩展系统盘:使用磁盘管理工具,将其他磁盘的空闲空间分配给C盘(需确保其他盘有未分配空间)。
  • 添加外接存储设备:将程序输出路径设置到外接硬盘或U盘上,利用外部存储承载大量数据。

内容的提问来源于stack exchange,提问作者Elly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 12:40:27