Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the path of the posted file in Python

Tags:

python

django

I am getting a file posting from a file:

file = request.post['ufile']

I want to get the path. How can I get it?

like image 683
Rajasekar Avatar asked Jul 15 '11 06:07

Rajasekar


1 Answers

You have to use the request.FILES dictionary.

Check out the official documentation about the UploadedFile object, you can use the UploadedFile.temporary_file_path attribute, but beware that only files uploaded to disk expose it (that is, normally, when using the TemporaryFileUploadHandler uploads handler).

upload = request.FILES['ufile']
path = upload.temporary_file_path

In the normal case, though, you would like to use the file handler directly:

upload = request.FILES['ufile']
content = upload.read()  # For small files
# ... or ...
for chunk in upload.chunks():
    do_somthing_with_chunk(chunk)  # For bigger files
like image 91
GaretJax Avatar answered Oct 11 '22 14:10

GaretJax