title: Python error : Python ValueError: I/O operation on closed file
body: <p>Python error : Python ValueError: I/O operation on closed file</p>
  
  <p>I am trying code </p>
  
  <pre><code>import random
  
  with open("GoneGirlSW.txt", "r") as f,open('trainData.txt','w') as   trainFile,open('testData.txt','w') as testFile:
      data = f.read().split('\n')
      random.shuffle(data)
      train_data = data[:50]
      test_data = data[50:]
  
      trainFile.write(str(train_data))
      testFile.write(str(test_data))
  </code></pre>
  
  <p>But it is giving error</p>
  
  <pre><code>Traceback (most recent call last):
    File "trainTest.py", line 9, in &lt;module&gt;
      trainFile.write(str(train_data))
  ValueError: I/O operation on closed file.
  </code></pre>