ValueError: I/O operation on closed file with python cStringIO

10,581

You are using x in a loop, but you close it the the first time you hit your max size. When the loop goes around the next time, the StringIO is closed and can't be used anymore.

You should create the StringIO inside the function, not outside it. Move the x = StringIO() inside the function body (before the loop), and then change your last line to x = StringIO() so that you create a new StringIO every time you fill up an old one.

Share:
10,581
jkdba
Author by

jkdba

I am here to learn and help when and where I can.

Updated on June 04, 2022

Comments

  • jkdba
    jkdba almost 2 years

    Ok so I have a function that writes data to memory. I am trying to let the data get to a set amount of size (in this case one gb) then truncate/dump to a file and continue going in this process until this is no more data to write. Currently it will get to the limit and truncate it then fails with one of the following errors depending on how I end the function.

    Traceback (most recent call last):
      File "<pyshell#192>", line 1, in <module>
        cart_for(10,'1234567890')
      File "<pyshell#191>", line 8, in cart_for
        x.write(''.join(p))
    ValueError: I/O operation on closed file
    

    my function is:

    x = StringIO()
    def cart_for(n, seq):
    b = 8
    a = 100000
    size_max = 1073741824
    for m in range(n, b, -1):
        a = a - 1
        for p in itertools.product(seq, repeat=m-1):
            x.write(''.join(p))
            x.write('\n')
            x.seek(0, os.SEEK_END)
            size = x.tell()
            if size > size_max:
                with open('C:/out/' + ran(8,string.ascii_letters) + '.txt', 'w') as handle:
                    handle.write(x.getvalue())
                    x.close()
                    x
    

    If I end my function with:

    x = StringIO()
    

    instead of:

    x
    

    The error is now:

    Traceback (most recent call last):
      File "<pyshell#189>", line 1, in <module>
        cart_for(10, '1234567890')
      File "<pyshell#188>", line 8, in cart_for
        x.write(''.join(p))
    UnboundLocalError: local variable 'x' referenced before assignment
    

    Any ideas on how to complete this process? As a reference as I am sure many of you can assume I want to truncate the memory to disk every so often so I do not run out of space in the memory so the function can continue running until it is complete. Is it possible to create random variable names defined by a static variable to complete this (if that makes sense)? forgive me I am new to python.