【问题标题】:csv.reader.close() raises an AttributeErrorcsv.reader.close() 引发 AttributeError
【发布时间】:2014-12-16 23:31:08
【问题描述】:

使用下面的代码我得到了错误:

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'Student Data.csv'

但是如果我添加 r.close() 它会返回错误:

AttributeError: '_csv.reader' object has no attribute 'close'

键盘代码为2580,CSV格式为

bakerg,ict,George Baker,11HM,NORMAL

代码:

from tkinter import *
import csv
import os
login = "bakerg"

def upgradetoadmin():
    global masterpassword
    masterpassword = []
    def one():
        masterpassword.append("1")
        arraycheck()
    def two():
        masterpassword.append("2")
        arraycheck()
    def three():
        masterpassword.append("3")
        arraycheck()
    def four():
        masterpassword.append("4")
        arraycheck()
    def five():
        masterpassword.append("5")
        arraycheck()
    def six():
        masterpassword.append("6")
        arraycheck()
    def seven():
        masterpassword.append("7")
        arraycheck()
    def eight():
        masterpassword.append("8")
        arraycheck()
    def nine():
        masterpassword.append("9")
        arraycheck()
    def zero():
        masterpassword.append("0")
        arraycheck()
    def clear():
        global masterpassword
        masterpassword = []
    def arraycheck():
        global masterpassword
        if len(masterpassword) == 4:
            if masterpassword == ['2','5','8','0']:
                print("Success")
                r = csv.reader(open('Student Data.csv'))
                lines = [l for l in r]
                print(lines)
                i = 0
                for item in lines:
                    if item[0] == login:
                        print(item)
                        print("YAY")
                        item[4] = "ADMIN"
                        print(item)
                        os.remove('Student Data.csv')
                        writer = csv.writer(open('Student Data.csv', 'w'))
                        writer.writerows(lines)

                print(login + " is now an admin")
            else:
                print("Invalid Code")
            masterpassword = []

    keypadwindow = Tk()
    keypadwindow.iconbitmap("hXYTZdJy.ico")
    keypadwindow.title("ADMIN UPGRADER")
    Button(keypadwindow, text="1", height = 4, width = 10, command = one).grid(column = 0, row = 0)
    Button(keypadwindow, text="2", height = 4, width = 10, command = two).grid(column = 1, row = 0)
    Button(keypadwindow, text="3", height = 4, width = 10, command = three).grid(column = 2, row = 0)
    Button(keypadwindow, text="4", height = 4, width = 10, command = four).grid(column = 0, row = 1)
    Button(keypadwindow, text="5", height = 4, width = 10, command = five).grid(column = 1, row = 1)
    Button(keypadwindow, text="6", height = 4, width = 10, command = six).grid(column = 2, row = 1)
    Button(keypadwindow, text="7", height = 4, width = 10, command = seven).grid(column = 0, row = 2)
    Button(keypadwindow, text="8", height = 4, width = 10, command = eight).grid(column = 1, row = 2)
    Button(keypadwindow, text="9", height = 4, width = 10, command = nine).grid(column = 2, row = 2)
    Button(keypadwindow, text="0", height = 4, width = 10, command = zero).grid(column = 1, row = 3)
    Button(keypadwindow, text="CLEAR", height = 4, width = 10, command = clear).grid(column = 2, row = 3)
    keypadwindow.mainloop()

upgradetoadmin()

【问题讨论】:

    标签: python csv python-3.x


    【解决方案1】:

    csv.reader 没有close 方法(或任何其他常规方法)。相反,它接受一个文件对象作为参数并对其进行迭代,逐一生成行。

    您应该在文件对象本身上调用close()

    my_file = open('Student Data.csv')
    r = csv.reader(my_file)
    ...
    my_file.close()
    

    当然,with-statement 会自动为您执行此操作:

    with open('Student Data.csv') as my_file:
        r = csv.reader(my_file)
        ...
    

    【讨论】:

    • 谢谢,真正的帮助! :D
    【解决方案2】:

    你需要关闭open返回的file对象而不是csv.reader

    使用with 块为您关闭文件:

    with open('Student Data.csv') as f:
        r = csv.reader(f)
        # ...
    

    【讨论】:

      【解决方案3】:

      我认为你应该有这个:

                      f = open('Student Data.csv', 'w')
                      writer = csv.writer(f)
                      writer.writerows(lines)
                      f.close() 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-12
        • 1970-01-01
        • 1970-01-01
        • 2021-12-08
        • 2013-02-08
        • 1970-01-01
        相关资源
        最近更新 更多