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

footballdata包报FileExistsError:Windows文件已存在无法创建错误求助

解决footballdata包read_games方法的WinError 183文件已存在错误

问题背景

使用footballdata包调用read_games()时触发FileExistsError: [WinError 183] Cannot create a file when that file already exists,尽管包源码声称支持覆盖已有数据,且已提前用pathlib创建目标目录并设置exist_ok=True,问题仍未解决。

可行解决方案

1. 手动清理目标缓存文件

Windows系统下,footballdata的文件覆盖逻辑存在兼容性问题。找到包存储联赛数据的缓存文件(通常在你指定的C:\\Users\\username\\.spyder-py3\\data目录,或包默认的用户缓存目录C:\\Users\\username\\.cache\\footballdata),手动删除对应赛季(22赛季)的文件后,重新运行代码。

2. 显式传递覆盖参数

检查read_games()或MatchHistory初始化的参数,确认是否有overwrite类的开关。若存在,显式设置为True强制覆盖已有文件,修改代码如下:

import numpy as np
import pandas as pd
import requests
import unidecode
import footballdata as foo
import pathlib

pathlib.Path('C:\\Users\\username\\.spyder-py3\\data').mkdir(parents=True, exist_ok=True)

# 若覆盖参数在read_games中
EPL = foo.MatchHistory('ENG-Premier League', [22]).read_games(overwrite=True)
# 若覆盖参数在MatchHistory初始化时传入
# EPL = foo.MatchHistory('ENG-Premier League', [22], overwrite=True).read_games()
print(EPL.head())

3. 适配Windows修改包源码

如果上述方法无效,可直接修改包的文件处理逻辑:

  1. 找到footballdata包的安装路径(可通过import footballdata; print(footballdata.__file__)查看)
  2. 定位到处理文件创建/写入的代码段,替换为兼容Windows的覆盖逻辑:
    • 将os.makedirs(path)改为os.makedirs(path, exist_ok=True)
    • 或在写入文件前先删除已有文件:
      import os
      target_file = "目标文件路径"
      if os.path.exists(target_file):
          os.remove(target_file)
      # 执行后续文件创建/写入操作
      

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 22:56:30