title: ValueError: I/O operation on closed file with python cStringIO
body: <p>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.</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;pyshell#192&gt;", line 1, in &lt;module&gt;
      cart_for(10,'1234567890')
    File "&lt;pyshell#191&gt;", line 8, in cart_for
      x.write(''.join(p))
  ValueError: I/O operation on closed file
  </code></pre>
  
  <p>my function is:</p>
  
  <pre><code>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 &gt; size_max:
              with open('C:/out/' + ran(8,string.ascii_letters) + '.txt', 'w') as handle:
                  handle.write(x.getvalue())
                  x.close()
                  x
  </code></pre>
  
  <p>If I end my function with:</p>
  
  <pre><code>x = StringIO()
  </code></pre>
  
  <p>instead of:</p>
  
  <pre><code>x
  </code></pre>
  
  <p>The error is now:</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;pyshell#189&gt;", line 1, in &lt;module&gt;
      cart_for(10, '1234567890')
    File "&lt;pyshell#188&gt;", line 8, in cart_for
      x.write(''.join(p))
  UnboundLocalError: local variable 'x' referenced before assignment
  </code></pre>
  
  <p>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.</p>