How can I get uploaded text file in view through Django?
<p>I'm now making web app. This app gets text file having not-organized data and organize it. I'm now using Django in Python3.</p>

<p>I already made form data in templates.</p>

<ul>
<li>Teplates</li>
</ul>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>&gt;    &lt;form action="/practice/kakao_reader/" method="post"enctype="multipart/form-data"&gt;{% csrf_token %}
&gt;        File:
&gt;        &lt;input type="file" name="file"/&gt;
&gt;        &lt;input type="submit" value="UPLOAD" /&gt;
&gt;     &lt;/form&gt;</code></pre>
</div>
</div>
</p>

<p>But I have difficulty in getting uploaded file through VIEW.
The first code that I've tried was </p>

<ul>
<li>View.py</li>
</ul>

<blockquote>
  <p>def kakao_reader(request):</p>
  
  <p>f = codecs.open(request.FILES['file'], encoding = 'utf-8') </p>
  
  <p>data = f.read()</p>
</blockquote>

<p>And I get  <strong><em>invalid file: InMemoryUploadedFile:</em></strong> this error.</p>

<p>The specific Error is</p>

<blockquote>
  <p>Environment:</p>
  
  <p>Request Method: POST Request URL:
  <a href="http://localhost:8000/practice/kakao_reader/" rel="nofollow">http://localhost:8000/practice/kakao_reader/</a></p>
  
  <p>Django Version: 1.10 Python Version: 3.5.2 Installed Applications:
  ['django.contrib.admin',  'django.contrib.auth', 
  'django.contrib.contenttypes',  'django.contrib.sessions', 
  'django.contrib.messages',  'django.contrib.staticfiles', 
  'elections',  'practice'] Installed Middleware:
  ['django.middleware.security.SecurityMiddleware', 
  'django.contrib.sessions.middleware.SessionMiddleware', 
  'django.middleware.common.CommonMiddleware', 
  'django.middleware.csrf.CsrfViewMiddleware', 
  'django.contrib.auth.middleware.AuthenticationMiddleware', 
  'django.contrib.messages.middleware.MessageMiddleware', 
  'django.middleware.clickjacking.XFrameOptionsMiddleware']</p>
  
  <p>Traceback:</p>
  
  <p>File "C:\Python35\lib\site-packages\django\core\handlers\exception.py"
  in inner
    39.             response = get_response(request)</p>
  
  <p>File "C:\Python35\lib\site-packages\django\core\handlers\base.py" in
  _get_response
    187.                 response = self.process_exception_by_middleware(e, request)</p>
  
  <p>File "C:\Python35\lib\site-packages\django\core\handlers\base.py" in
  _get_response
    185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)</p>
  
  <p>File "C:\Django\mysite\practice\views.py" in kakao_json
    43.     f = codecs.open(request.FILES['file'], encoding = 'utf-8')</p>
  
  <p>File "C:\Python35\Lib\codecs.py" in open
    895.     file = builtins.open(filename, mode, buffering)</p>
  
  <p>Exception Type: TypeError at /practice/kakao_reader/ Exception Value:
  invalid file: </p>
</blockquote>

<p>How can I fix it? thank you.</p>

<p><code>request.FILES['file']</code> is already a file handler, so you don't have to open it. Just use <code>request.FILES['file'].read()</code>.</p>

python django python-3.x