PermissionError with open()

27 views
Skip to first unread message

John

unread,
Feb 2, 2015, 1:32:40 AM2/2/15
to django...@googlegroups.com
Hello everyone,

I am trying to run this code inside view.py, to make PDF file able to be downloaded :

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))    
def download(request, file_name = 'article1'):
        file = open(os.path.join(BASE_DIR, 'media').replace('\\','/').format(file_name), 'rb')
        response = HttpResponse(file, content_type='application/pdf')
        response['Content-Disposition'] = "attachment; filename={}".format(file_name)
        return response

The problem with line 4. I will get PermissionError on windows

PermissionError at /download/

[Errno 13] Permission denied: 'C:/Users/Oana/Desktop/tutela-net/media'

 

and IsADirectoryError on linux.

Even though, if I will use 'wb' instead of 'rb' or any other options. or the absolute path, I will get the same error.



Vijay Khemlani

unread,
Feb 2, 2015, 8:54:11 AM2/2/15
to django...@googlegroups.com
Your call to 

".format(file_name)"

does nothing as the original string does not have the positional arguments ("{0}" for example)

You could just append the filename with "+".

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/25e64be7-ed56-43bb-862e-76eeb3ecdfba%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages