【发布时间】:2018-01-07 19:28:14
【问题描述】:
我需要扫描代码中的第二项以检查其正确性,但返回此错误:
Traceback (most recent call last):
File "C:/Users/oscar/Documents/ff.py", line 29, in <module>
with open(account) as f:
TypeError: expected str, bytes or os.PathLike object, not 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])
【问题讨论】:
-
如果您的帐户是文件路径列表作为字符串,您需要做
for filepath in accounts: with open(filepath) as f -
这一行:
account = open("%s.txt" %username, "a+")已经在变量account中“放置”了一个打开的文件。然后你把打开的文件对象放在一个列表中并尝试再次打开它。 -
那么解决办法是什么
-
我尝试了 for filepath 的东西,但为此不得不删除“with open(account) as f:”
标签: python string list error-handling