title: Can't write to CSV in function
body: <p>This will return the exception &quot;ValueError: I/O operation on closed file&quot;</p>
  <pre><code>def clear():
      os.system('cls')
  def openFile(operation):
      global file
      global writer
      with open(r'C:\\Users\\User\Documents\\Proxy Database\\Proxy.csv',operation, newline= '') as file:
          writer = csv.writer(file)
  while True:
      dir = input('Enter Operation &gt; ')
      if dir == 'add':
          openFile(&quot;a&quot;)
          print('! Adding User !')
          name = input('Enter Name &gt; ')
          writer.writerow([name])
          print('User Added')
          file.flush()
          clear()
          continue
  </code></pre>
  <p>I notice that when I put all the code under the initial CSV Open everything works fine, it just dosen't work when put into a function.</p>
  <pre><code>with open(r'C:\\Users\\ricco\Documents\\Proxy Database\\Proxy.csv','r+', newline= '') as file:
          writer = csv.writer(file)
          while True:
              dir = input('Enter Operation &gt; ')
              if dir == 'add':
                  print('! Adding User !')
                  name = input('Enter Name &gt; ')
                  writer.writerow([name])
                  print('User Added')
                  file.flush()
                  break
          print('Done')
  </code></pre>