title: Python IOError: File not open for writing and global name 'w' is not defined
body: <p>I'm trying to write a little procedure that write (append would be even better) a line in a file with Python, like this: </p>
  
  <pre><code>def getNewNum(nlist):
      newNum = ''
      for i in nlist:
          newNum += i+' ' 
      return newNum
  
  def writeDoc(st):
      openfile = open("numbers.txt", w)
      openfile.write(st)
  
  newLine =  ["44", "299", "300"]
  
  writeDoc(getNewNum(newLine))
  </code></pre>
  
  <p>But when I run this, I get the error: </p>
  
  <pre><code>openfile = open("numbers.txt", w)
  NameError: global name 'w' is not defined
  </code></pre>
  
  <p>If I drop the "w" paremeter, I get this other error:</p>
  
  <pre><code>line 9, in writeDoc
      openfile.write(st)
  IOError: File not open for writing
  </code></pre>
  
  <p>I'm following exactly (I hope) what is <a href="http://www.sthurlow.com/python/lesson10/" rel="noreferrer">here</a>. </p>
  
  <p>The same occurs when I try to append the new line. How can I fix that?</p>