title: Permission denied when trying to write to file from a view
body: <p>I've created a chat for this question: <a href="https://chat.stackoverflow.com/rooms/4788/discussion-between-daniel-nill-and-balsagoth">here</a></p>
  <p>I have a view that attempts to execute <code>f = open('textfile.txt', 'w')</code> but on my live server this brings up the error <code>[Errno 13] Permission denied: 'textfile.txt'</code>.</p>
  <p>My file structure is as follows:</p>
  <pre><code>- root
      |
      - project
            |
            - app
            |
            - media
  </code></pre>
  <p>where the view lives in <code>app</code>.</p>
  <p>I have tried having textfile.txt live in root, project, app and media all of which have 777 file permissions (owner, group and public can read, write and execute)[*1].</p>
  <p>If I change the command to a read permission ie <code>f = open('textfile.txt', 'r')</code> I get the same error.</p>
  <p>My media root is set to <code>os.path.join(os.path.dirname(__file__), 'media').replace('\\','/')</code>
  and this is all running on an apache server through webfaction.</p>
  <p>So I have two questions.  Where is django/python trying to open this file from? and what do I need to change to get permission for the view to open and write to the file.</p>
  <p>[*1] I know this is not a good idea, I just have this set for current debugging purposes.</p>
  <hr />
  <p>EDIT:</p>
  <p>I don't know if this is relevant but now when I change it to <code>f = open(os.path.join(settings.MEDIA_URL, 'textfile.txt'), 'r')</code> rather than <code>f = open(os.path.join(settings.MEDIA_URL, 'textfile.txt'), 'w')</code> I get the error <code>[Errno 2] No such file or directory</code>.</p>
  <p>I don't know if this has meaning or not...</p>