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

Python开发预算管理App遇TypeError:传入方法而非路径对象

预算支出管理App TypeError问题解决

问题背景

作为Python新手开发预算/支出管理App,创建了Usuario用户类、列表加载类及w_jason类用于写入JSON文件,但实例化w_jason时触发错误:TypeError: expected str, bytes or os.PathLike object, not method。

相关代码

Usuario类代码

import os  
import json

class Usuario:
    def __init__(self,nombre):
        self.nombre = nombre
        self.verify_json()
        print(f"Te damos la bienvenida {self.nombre}")

    def get_nombre(self):
        return self.nombre
    
    def save_json(self,nombre):
        with open('data/'+nombre +"_usuario.json","w") as f:
            json.dump(self.nombre,f,indent=4)

    def verify_json(self):
        if not os.path.exists('data/'+self.nombre + '_usuario.json'):
            self.save_json(self.nombre)
        else:
            with open('data/'+self.nombre + '_usuario.json', 'r') as file:
                return json.load(file)
                
    def get_json(self):
        dir = 'data/'+ self.nombre + '_usuario.json'
        with open(dir, 'r') as file:
            return json.load(file)

w_jason类代码

class w_jason():
    def __init__(self,archivo_json,lista_cuentas, nombre):
        self.nombre = nombre
        self.archivo_json = archivo_json
        self.lista_cuentas = lista_cuentas
        self.meses = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio',
                      'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre']
        
        self.archivo_user = self.leer_json()
    
    def listas_in_json(self):
        self.cuentas_dict = {self.lista_cuentas[i]: [] for i in range(len(self.lista_cuentas))}
        self.meses_dict = {self.meses[i]: self.cuentas_dict for i in range(len(self.meses))}
        return self.meses_dict
    
    def leer_json(self):
        with open(self.archivo_json, 'r') as file:
            return json.load(file)
    
    def edit_json(self):      
       with open(self.archivo_json, 'w') as file:
            json.dump(self.archivo_user, file, indent=4)

报错栈

Traceback (most recent call last):
  File "/Users/jabac/Desktop/work/proyecto_cuenta/scripts/test.py", line 7, in <module>
    m_json = w_jason(mariana.get_json, marian_cuentas.get_lista_cuentas, mariana.get_nombre)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jabac/Desktop/work/proyecto_cuenta/scripts/write.py", line 19, in __init__
    self.archivo_user = self.leer_json()
                        ^^^^^^^^^^^^^^^^
  File "/Users/jabac/Desktop/work/proyecto_cuenta/scripts/write.py", line 28, in leer_json
    with open(self.archivo_json, 'r') as file:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: expected str, bytes or os.PathLike object, not method

问题分析与修复

问题根源

实例化w_jason时传递的是方法对象(如mariana.get_json)而非方法调用后的返回值,且w_jason的leer_json方法需要接收文件路径字符串,但Usuario的get_json返回的是解析后的JSON字典,参数类型完全不匹配。

修复方案

方案一:传递正确的文件路径字符串

  1. 在Usuario类中新增获取JSON文件路径的方法:
def get_json_path(self):
    return f'data/{self.nombre}_usuario.json'
  1. 修正test.py中的实例化代码,调用所有方法并传递正确参数:
m_json = w_jason(
    mariana.get_json_path(),  # 传递文件路径字符串
    marian_cuentas.get_lista_cuentas(),  # 调用方法获取列表数据
    mariana.get_nombre()  # 调用方法获取用户名
)

方案二:调整w_jason逻辑直接接收解析后的JSON数据

如果希望直接传入已解析的JSON数据,修改w_jason类:

class w_jason():
    def __init__(self, archivo_json, lista_cuentas, nombre):
        self.nombre = nombre
        self.archivo_user = archivo_json  # 直接使用传入的解析后数据
        self.lista_cuentas = lista_cuentas
        self.meses = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio',
                      'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre']
    
    def listas_in_json(self):
        self.cuentas_dict = {self.lista_cuentas[i]: [] for i in range(len(self.lista_cuentas))}
        self.meses_dict = {self.meses[i]: self.cuentas_dict for i in range(len(self.meses))}
        return self.meses_dict
    
    def edit_json(self):      
       file_path = f'data/{self.nombre}_usuario.json'
       with open(file_path, 'w') as file:
            json.dump(self.archivo_user, file, indent=4)

此时实例化代码改为:

m_json = w_jason(
    mariana.get_json(),
    marian_cuentas.get_lista_cuentas(),
    mariana.get_nombre()
)

关键提醒

调用方法必须加括号()才能获取返回值,否则传递的是方法对象本身;确保参数类型与函数/方法的需求匹配,不要混淆方法对象和返回值。

内容的提问来源于stack exchange,提问作者Joaquin Andrés Bächler Díaz

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 23:12:06