A..
A..

Reputation: 375

csv.reader.close() raises an AttributeError

With the below code I get the error:

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

BUT if i add r.close() it returns me with the error:

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

The code for the keypad is 2580 and the CSV is in the format of

bakerg,ict,George Baker,11HM,NORMAL

Code:

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()

Upvotes: 2

Views: 5911

Answers (3)

b4hand
b4hand

Reputation: 9770

You need to close the file object returned by open not the csv.reader.

Use a with block to close the file for you:

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

Upvotes: 2

Marcin
Marcin

Reputation: 238309

I think you should have this:

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

Upvotes: 1

user2555451
user2555451

Reputation:

csv.reader does not have a close method (or any other normal methods for that matter). Instead, it takes a file object as its argument and iterates over it, yielding the lines one-by-one.

You should be calling close() on the file object itself:

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

Of course, a with-statement will do this for you automatically:

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

Upvotes: 5

Related Questions