Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get uploaded file in views?

I’m trying to get uploaded data in my views. Firstly, I’m getting the path and after that I have to read the file but Django gives me an error

FileNotFoundError: [Errno 2] No such file or directory:
‘/Users/edc/PycharmProjects/wl/SM/uploads/meetings notes (1).docx

but I have that file. How can I fix that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

upload = Upload(file=f)
content = ScanDocument(upload.file.path)
upload.save()


def ScanDocument(file_path):
  text = docx2txt.process(file_path)
  return text

Note if I use url instead of path then it returns:

FileNotFoundError: [Errno 2] No such file or directory:
‘/media/Meeting%20notes%20notes%20%(1).docx’

>Solution :

If you check your file path in error it’s invalid if it’s uploaded inside media directory

'/Users/edc/PycharmProjects/wl/SM/uploads/meetings notes (1).docx'

just change you code like this

import os
from django.conf import settings


upload = Upload(file=f)
file_path = os.path.join(settings.MEDIA_ROOT, upload.file.path)
content = ScanDocument(file_path)
upload.save()


def ScanDocument(file_path):
    text = docx2txt.process(file_path)
    return text
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading