Home > Software design >  I/O operation on closed file when the file is not closed
I/O operation on closed file when the file is not closed

Time:12-19

I have written a piece of code in Python which needs to check a few arrays and ensure certain values are not in there. These values are in the form of variables which I defined like so:

    tempArray = "".join(map(str,array))

    dashTemp = tempArray[0:11]
    dotTemp = tempArray[11:23]
    spaceTemp = tempArray[23:35]

    dashTemp = "".join(sorted(dashTemp))
    dotTemp = "".join(sorted(dotTemp))
    spaceTemp = "".join(sorted(spaceTemp))

I have then attempted to check whether these values are already in existing text files that I have set up (which I opened earlier):

    if array in f or dashTemp in fa or dotTemp in fo or spaceTemp in fs:
        f.close()

The above code works perfectly fine, however it is when I change it to the following that it does not work:

    if array in f or dashTemp in fa or dotTemp in fo or spaceTemp in fs or "6" in spaceTemp or "o" in spaceTemp:
        f.close()

I am given the error "ValueError: I/O operation on closed file." which I do not understand, because what I added has nothing to do with a file; the spaceTemp variable is the only new thing referenced. I could even understand if the error was located on the line f.close(), but it is not.

File "...", line 76, in gen
if array in f or dashTemp in fa or dotTemp in fo or spaceTemp in fs or "6" in spaceTemp or "o" in spaceTemp: ValueError: I/O operation on closed file.

As I have mentioned, all three files (f, fa, fo and fs) are all open, and this works without the added "or "6" in spaceTemp...".

Any ideas?

More context:

f = open("iterations.txt","r")
fa = open("dashes.txt","r")
fo = open("dots.txt","r")
fs = open("spaces.txt","r")
while restart == True:
    while count < 36:
        characterAdd = chars[random.randint(0,35)]
        if not characterAdd in array:
            array.append(characterAdd)
            count  = 1
        else:
            pass
    tempArray = "".join(map(str,array))
    dashTemp = tempArray[0:11]
    dotTemp = tempArray[11:23]
    spaceTemp = tempArray[23:35]
    dashTemp = "".join(sorted(dashTemp))
    dotTemp = "".join(sorted(dotTemp))
    spaceTemp = "".join(sorted(spaceTemp))
    if array in f or dashTemp in fa or dotTemp in fo or spaceTemp in fs  or "6" in spaceTemp or "o" in spaceTemp:
        f.close()

CodePudding user response:

Try to re-write your code using the with statement. That saves you a lot of headaches about closing files.

CodePudding user response:

This looks like you're testing that something is true in the files. In that case, read the files to a variable and test the variables

with open("iterations.txt","r") as f:
    iterations_text = f.read()

with open("dashes.txt", "r") as f:
   dashes_text = f.read()

did_the_bad_thing_happen = False

while restart == True:
    while count < 36:
        characterAdd = chars[random.randint(0,35)]
        if not characterAdd in array:
            array.append(characterAdd)
            count  = 1
        else:
            pass
    tempArray = "".join(map(str,array))
    dashTemp = tempArray[0:11]
    dotTemp = tempArray[11:23]
    spaceTemp = tempArray[23:35]
    dashTemp = "".join(sorted(dashTemp))
    dotTemp = "".join(sorted(dotTemp))
    spaceTemp = "".join(sorted(spaceTemp))
    if array in iterations_text or dashTemp in dashes_text or ...:
        did_the_bad_thing_happen = True
        break  #  also, this might solve your problem!

If you're determined to go down your path, you should break out of the loop as soon as you close the filestream using break like in the above example.

  • Related