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

调用Read_file类方法时提示缺少必填参数fileName的问题求助

解决方法

这个错误是因为直接用类名调用实例方法导致的,实例方法的第一个参数self需要类的实例来填充,没创建实例就调用自然会提示缺少参数。下面给两种可行的修复方式:

方式一:先创建类实例再调用方法

修改代码最后的调用部分,先实例化Read_file类,再调用方法:

# 创建类实例
reader_instance = Read_file()
# 通过实例调用方法
file_read = reader_instance.file_reading_through_class('./ConfigurationFile/configFile.csv')

方式二:把方法改为静态方法

如果不想创建实例,可以给方法加上@staticmethod装饰器,同时去掉方法的self参数(静态方法不需要实例关联):

import pandas as pd
import sys,os, time,re
   
class Read_file():
    @staticmethod
    def file_reading_through_class(fileName):
        if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
            path_actual = os.getcwd()
            path_main_folder = path_actual[:-4]
            path_result = path_main_folder + fileName
            print('frozen path', os.path.normpath(path_result))
            file_to_read = pd.read_json(path_result)
            return file_to_read
        else:
            file_to_read = pd.read_json(fileName)
            return file_to_read
   
# 直接通过类名调用静态方法
file_read=Read_file.file_reading_through_class('./ConfigurationFile/configFile.csv')

额外提醒

你代码里用pd.read_json读取.csv文件,这会导致后续报错,CSV文件应该用pd.read_csv()来读取,记得替换对应的方法。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 10:30:11