Python-TypeError:参数必须有一个";写下;方法

Python-TypeError:参数必须有一个";写下;方法,python,csv,Python,Csv,您好,这是我的代码: def update_thing(): stud_ID = str(ID_num.get()) stud_name = str(name.get()) stud_course = str(Crs.get()) stud_year = str(Yr.get()) replace = stud_ID +','+ stud_name +','+ stud_course +','+ stud_year

您好,这是我的代码:

def update_thing():
        stud_ID = str(ID_num.get())
        stud_name = str(name.get())
        stud_course = str(Crs.get())
        stud_year = str(Yr.get())
        replace = stud_ID +','+ stud_name +','+ stud_course +','+ stud_year
        empty = []




        with open(file_op, 'wb') as fop:
            Swriter = csv.writer(file_op, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
            for row in fop:
                if row[0:9] == stud_ID:

                    Swriter.writerow([empty])
                    Swriter.writerow([replace])
                    msg = Label(upd_win, text="Updated Successful", font="fixedsys 12 bold").place(x=3,y=120)
                if not row[0:9] == getID:
                    msg1 = Label(upd_win, text="Updated Failed", font="fixedsys 12 bold").place(x=3,y=120)


        fop.close() 
此代码替换通过ID号找到的行。因此,如果它找到了ID号,它将通过清空ID号并写入新输入的行来替换这些行。然而,我有一个错误的在线问题

Swriter = csv.writer(file_op, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
它有一个错误:TypeError:参数必须有一个“write”方法。我真的不知道为什么我会得到这个

file_op 
是我从GUI中输入的值中获取的文件名。非常感谢您的帮助。谢谢

打开“fop”文件,但试图写入“file_op”

你需要这样做:

with open("your filename string", 'wb') as file_op:
            Swriter = csv.writer(file_op, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)

fop.close()
是不必要的,顺便说一句,我想您应该将
fop
作为第一个参数传递给
csv.writer()
,而不是
file\u op