gpt4 book ai didi

python - TypeError: 'list' 不支持缓冲区接口(interface)

转载 作者:太空宇宙 更新时间:2023-11-03 17:21:58 24 4
gpt4 key购买 nike

我有一个程序,会询问用户 10 个问题,然后将其保存到文件中。所有这些都工作得很好,但是我随后尝试读取一组已保存的文件,这会引发上述类型错误。

导致问题的代码是这样的:

def readfile():
classtoload = input("Which class would you like to load: ")
gclass = "Class_" + classtoload
script_dir = os.path.dirname(os.path.abspath(__file__))
globclass = os.path.join(script_dir, gclass)
list_of_files = glob.glob(globclass + '\*.pkl')
files = []
files.append(list_of_files)
print(files)
for s in files:
load = os.path.dirname(os.path.abspath(s))
pickle.load(load)

完整的错误是这样的:

Traceback (most recent call last):
File "C:\Users\Liam\Documents\LIWA HW\Python programs\maths question.py", line 102, in <module>
ts()
File "C:\Users\Liam\Documents\LIWA HW\Python programs\maths question.py", line 10, in ts
readfile()
File "C:\Users\Liam\Documents\LIWA HW\Python programs\maths question.py", line 96, in readfile
load = os.path.dirname(os.path.abspath(s))
File "C:\Users\Liam\Documents\LIWA HW\python\lib\ntpath.py", line 547, in abspath
path = _getfullpathname(path)
TypeError: 'list' does not support the buffer interface

我的完整代码是这样的:

import random, re, pickle, os, glob

def ts():
tors = ""
while tors not in ["T","S"]:
tors = input("are you a teacher or student: ").upper()
if tors == "S":
name_enter()
else:
readfile()

def name_enter():
global forename, surname
forename, surname = "", ""
while forename == "" or len(forename) > 25 or not re.match(r'^[A-Za-z0-9-]*$', forename):
forename = input("Please enter your forename: ")
while surname == "" or len(surname) > 30 or not re.match(r'^[A-Za-z0-9-]*$', surname):
surname = input("Please enter your surname: ")
enter_class()


def enter_class():
global class_choice
class_choice = None
while class_choice not in ["1","3","2"]:
class_choice = input("Please enter you class (1, 2, 3): ")
print("\nClass entered was " + class_choice)
mathsquestion()

def mathsquestion():
global qa, score
qa, score = 0, 0
for qa in range(0,10):
qa = qa + 1
print("The question you are currently on is: ", qa)
n1, n2, userans = random.randrange(12), random.randrange(12), ""
opu = random.choice(["-","+","x"])
if opu == "+":
while userans == "" or not re.match(r'^[0-9,-]*$', userans):
userans = input("Please solve this: %d" % (n1) + " + %d" % (n2) + " = ")
prod = n1 + n2
elif opu == "-":
while userans == "" or not re.match(r'^[0-9,-]*$', userans):
userans = input("Please solve this: %d" % (n1) + " - %d" % (n2) + " = ")
prod = n1 - n2
else:
while userans == "" or not re.match(r'^[0-9,-]*$', userans):
userans = input("Please solve this: %d" % (n1) + " x %d" % (n2) + " = ")
prod = n1 * n2
userans = int(userans)
prod = int(prod)
if prod == userans:
score = score + 1
print("Well done, you have got the question correct. Your score is now: %d" % (score))
else:
print("Unfortunatly that is incorrect. The answer you entered was %d" % (userans) + " and the answer is actually %d" % (prod))

print("Your final score is: %d" % (score))
savefile()

def savefile():
file = forename + "_" + surname + ".pkl"
script_dir = os.path.dirname(os.path.abspath(__file__))
dest_dir = os.path.join(script_dir,'Class_' + class_choice)
scoresave = {"%d" % score}
try:
os.makedirs(dest_dir)
except OSError:
pass
path = os.path.join(dest_dir, file)
with open(path, 'ab') as stream:
pickle.dump(scoresave, stream)
lists = []
infile = open(path, 'rb')
while True:
try:
lists.append(pickle.load(infile))
except EOFError:
break
obj=lists[0]
while len(lists) > 3:
lists.pop(0)
print(lists)
infile.close()

def readfile():
classtoload = input("Which class would you like to load: ")
gclass = "Class_" + classtoload
script_dir = os.path.dirname(os.path.abspath(__file__))
globclass = os.path.join(script_dir, gclass)
list_of_files = glob.glob(globclass + '\*.pkl')
files = []
files.append(list_of_files)
print(files)
for s in files:
load = os.path.dirname(os.path.abspath(s))
pickle.load(load)

最佳答案

files 是一个列表列表,因为您将名称列表附加到其中:

list_of_files = glob.glob(globclass + '\*.pkl')
files = []
files.append(list_of_files)

您现在拥有一个包含一个元素的列表,另一个列表。因此,当迭代 files 时,您会再次获得该列表:

for s in files:
load = os.path.dirname(os.path.abspath(s))

失败是因为 s 应该是一个字符串。

使用list.extend()代替:

files.extend(list_of_files)

或者更好的是,直接使用list_of_files:

for s in list_of_files:

关于python - TypeError: 'list' 不支持缓冲区接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33054910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com