title: Opening a data file from the media directory in Django
body: <p>I have an application that allows for users to upload CSV files with data which is then graphed and displayed to the user. These files are saved as media within my media folder. In my graph view however I need to open the file and process it. My problem is that I can only open files that are within my project's current working directory and any time that I attempt to upload a file from somewhere outside of that directory I get this error:</p>
  
  <pre><code>File b'TEST.CSV' does not exist
  </code></pre>
  
  <p>I have attempted this, but without success:</p>
  
  <pre><code>file_upload_dir = os.path.join(settings.MEDIA_ROOT, 'Data_Files')
  data_file = open(os.path.join(file_upload_dir, new_file), 'rb')
  </code></pre>
  
  <p>The variable <code>new_file</code> is only the name of the file saved from in a session and not a path to that file. <code>Data_Files</code> is a directory within the media directory that contains the uploaded files.</p>
  
  <p>My media settings for Django are</p>
  
  <pre><code>SETTINGS_DIR = os.path.dirname(__file__)
  PROJECT_PATH = os.path.join(SETTINGS_DIR, os.pardir)
  PROJECT_PATH = os.path.abspath(PROJECT_PATH)
  
  MEDIA_URL = '/media/'
  MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')
  </code></pre>
  
  <p>Is there a way to reference the media files properly from a view?</p>
  
  <p>Here is the output of <code>file_upload_dir</code> and the location of the new file.</p>
  
  <pre><code>&gt;&gt;&gt; print(file_upload_dir)
  C:\\Users\\vut46744\\Desktop\\graphite_project\\media\\Data_Files
  
  &gt;&gt;&gt; print(os.path.join(file_upload_dir, new_file))
  C:\\Users\\vut46744\\Desktop\\graphite_project\\media\\Data_Files\\TEST.CSV
  </code></pre>