TypeError:预期的 str、bytes 或 os.PathLike 对象,而不是方法,试图获取存档

问题描述 投票:0回答:1

我目前正在尝试构建和应用程序来管理预算或费用, 我对Python和一切都很陌生, 所以我从朋友那里得到了帮助,但我不太熟悉编码所涉及的所有过程。 因此,我为用户创建了一个类,另一个用于加载列表,另一个用于在用户类中创建的 json 上写入(注入)列表。我希望我解释清楚了。

这是用户代码:

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)

我已经更改了几次,因为当我从另一个脚本调用 json 文件时,我收到错误,它不是 str。 现在,我从这里称呼它:

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)
    

该类接收已创建的 json 存档的路径,但我收到错误:


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

所以,我不知道还能做什么。我返回了带有文件路径的 str,但它也给了我同样的错误,现在我尝试只返回 json 文件,但仍然收到错误。

该应用程序几分钟前就开始工作了,我不知道我做了什么改变了这种行为。

很抱歉,如果这太长了, 提前谢谢!

json python-3.x class typeerror readfile
1个回答
0
投票

我解决了,抱歉,我在调用该方法后没有添加 () 。

© www.soinside.com 2019 - 2024. All rights reserved.