Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将其作为列表阅读?_Python_String_List_Error Handling - Fatal编程技术网

Python 如何将其作为列表阅读?

Python 如何将其作为列表阅读?,python,string,list,error-handling,Python,String,List,Error Handling,我需要扫描代码中的第二项以检查其正确性,但返回时出现以下错误: 回溯(最近一次呼叫最后一次): 文件“C:/Users/oscar/Documents/ff.py”,第29行,在 已开立(账户)为f: TypeError:应为str、bytes或os.PathLike对象,而不是list 守则: step = str(input("do you have an account? (reply with yes or no)")) if step == ("no"): username

我需要扫描代码中的第二项以检查其正确性,但返回时出现以下错误:

回溯(最近一次呼叫最后一次):
文件“C:/Users/oscar/Documents/ff.py”,第29行,在
已开立(账户)为f:
TypeError:应为str、bytes或os.PathLike对象,而不是list
守则:

step = str(input("do you have an account? (reply with yes or no)"))

if step == ("no"):
    username = input("what do you want your username to be?")
    password = input("what do you want your password to be?")
    favgenre = input("what is your favourite genre?")
    favartist = input("who is your favourite artist?")
    account = open("%s.txt" %username, "w+")
    account.write(username)
    account.write(",")
    account.write(password)

    account.write(",")
    account.write(favgenre)
    account.write(",")
    account.write(favartist)
    account.write(",")
    account.close()

if step == ("yes"):
    username = input("please enter username")
    password = input("please enter your password")
    account = open("%s.txt" %username, "a+")
    account = [account]
    filename = (username)

    if username == username:
        with open(account) as f:
            strings = f.readlines()
            data = [string.split() for string in strings]
            print(data[1])
我将使用该模块来保存数据,而不是为每个用户创建单独的文件,而是将它们全部保存在字典中并保存

我的意思是:

import pickle

# First read any existing database into memory.
#
database_filename = 'user_db.pkl'
db_updated = False  # Flag indicating database needs to rewritten.
# Read the existing database if any, otherwise create an empty version.
try:
    with open(database_filename, 'rb') as infile:
        database = pickle.load(infile)
except FileNotFoundError:
    database = {}
    db_updated = True

# Then interact with user or do other operations to it.
#
step = str(input("Do you have an account (yes or no)? "))

if step == "no":
    username = input("What do you want your username to be? ")
    password = input("What do you want your password to be? ")
    favgenre = input("What is your favourite genre? ")
    favartist = input("Who is your favourite artist? ")

    data = [password, favgenre, favartist]
    database[username] = data
    db_updated = True

elif step == "yes":
    username = input("Please enter your username: ")
    if username not in database:
        print('Error: username {!r} not found in database'.format(username))
    else:
        password = input("OK, please enter your password: ")

        data = database.get(username)
        if data[0] != password:
            print('Error: password does not match')
            del data
        else:
            print('username {!r} found, data: {}'.format(username, data))

else:
    print('Please enter "yes" or "no"')

if db_updated:
    with open(database_filename, 'wb') as outfile:
        pickle.dump(database, outfile, pickle.HIGHEST_PROTOCOL)
        print('Database file updated')

在我的问题解答中还有关于pickle的信息。

如果您的帐户是文件路径列表中的字符串,您需要对帐户中的文件路径执行
:将open(filepath)作为f
这一行:
account=open(“%s.txt”%username,“a+”)
已经将打开的文件“放入”变量
account
。然后,您将打开的文件对象放入一个列表中,并尝试再次打开该对象。那么,修复的方法是什么呢?我尝试了文件路径的方法,但为此必须删除“以打开(帐户)作为f:”