Home > Net >  Why am i getting this error: AttributeError: 'list' object has no attribute 'load
Why am i getting this error: AttributeError: 'list' object has no attribute 'load

Time:01-07

When ever I try to load data from a json file I get this error: AttributeError: 'list' object has no attribute 'load'

What I expected

import json
users=[]
with open('username project.json','w') as f:
    jfile=json.dump(users,f)

def create_usernames():
    username=input('Enter in username')
    with open('username project.json','r ') as read:
        usersj=users.load(read)
    if username in users[usersj]:
            print('username rejected')
    else:
        print('username is okay')
create_usernames()

What resulted

usersj=users.load(read)
AttributeError: 'list' object has no attribute 'load'

CodePudding user response:

There're couple of typos in your code, and I corrected accordingly

import json
users=[]
with open('username project.json','w') as f:
    jfile=json.dump(users,f)

def create_usernames():
    username=input('Enter in username')
    with open('username project.json','r ') as read:
        usersj=json.load(read) # <-- updated this
    if username in usersj: # <-- and this
            print('username rejected')
    else:
        print('username is okay')
create_usernames()

Tested both the scenarios where a username exists and does not exist

CodePudding user response:

import json
users=[]
with open('username project.json','w') as f:
    jfile=json.dump(users,f)

def create_usernames():
    username=input('Enter in username')
    with open('username project.json','r ') as users: #changed this to users
       usersj=users.load(read)
    if username in users[usersj]:
            print('username rejected')
    else:
        print('username is okay')
create_usernames()

are you trying to load the json users? because list doesn't have any load method

  • Related