OSError: [Errno 9] Bad file descriptor returning even though file closed using .close()

179 Views Asked by At

I am currently trying out my hand at writing and reading from files. Here is the problematic part of the code :

def ldi(regAdress, value):
    buffer = open(regAdress, "w")
    buffer.write(bin(value))
    buffer.close()
def rdi(regAdress):
    buffer = open(regAdress, "r")
    print(int(buffer.read(10)))
    buffer.close()

As you can see, the first function properly closes to file before ending. However, when the second is called, it returns the previously mentionned error. I read that this was meant to me that the file was still open, and that my program was trying to open it again, causing an error.

TL;DR : Why is it still returning this error even though the file is closed ?

I tried putting the function contents inside of a try, finally block, with the first two lines of each inside of the try, and the final inside the finally, but it simply returns that "buffer" was referenced before assignement.

0

There are 0 best solutions below