title: Python won't write to file unless it already exists: permissions related issue
body: <p>I've got the following Python code:</p>
  
  <pre><code>class Server(object):
  
    def __init__(self, options):
      self.data_file = '/etc/my_module/resources.json'
  
    # Triggered when new data is received
    def write(self, data):
      try:
        f = open(self.data_file, 'w')
        json.dump(data, f)
      except Exception, e:
        print e
      finally:
        f.close()
  </code></pre>
  
  <p>If the file <code>/etc/my_module/resources.json</code> does not exist then I get an error message when I call the <code>write</code> method:</p>
  
  <pre><code>[Errno 2] No such file or directory: '/etc/my_module/resources.json'
  </code></pre>
  
  <p>If I create the file manually and call the write method again, then everything works and the json is written to the file.</p>
  
  <p>I'm running my application with sudo: <code>sudo python my_module.py</code>, and I've added my user (vagrant) to <code>/etc/sudoers</code>. </p>
  
  <p>So python won't create the file if it doesn't exist, on the grounds that it doesn't have permission to write to /etc, but it will happily write to the file if it does exist. Why is this?</p>