title: Write to File open in Second Function (Python)
body: <p>I currently have the below function within my code:-</p>
  
  <pre><code>def openFiles():
      file1 = open('file1.txt', 'w')
      file2 = open('file2.txt', 'w')
  </code></pre>
  
  <p>What I'm hoping to do is now, in a second method is to write to the open file. However, whenever I try to write to the files using for example "file1.write("hello")", an error is returned informing me that <code>"global variable 'file1' is not defined"</code>. I've tried declaring 'file1' as a string at the beginning of my code but obviously, as it isn't a string but an object, I'm unsure how to write to it.</p>
  
  <p>Any suggestions? I want a number of functions to have access to the files hence why I'd like a separate function that opens them.</p>
  
  <p>Thanks</p>
  
  <p><strong>Edited to represent a Class</strong></p>
  
  <pre><code>class TestClass:
      def openFiles():
          file1 = open('file1.txt', 'w')
          file2 = open('file2.txt', 'w')
  
      def write_to_files():
          ????????
  </code></pre>