Reputation: 1353
I'm doing a form to upload some files to my system and then work with it. First, upload the file is working well but when I want to change the extension of my uploaded file crashes.
Below I show my function,
from django.core.files.storage import FileSystemStorage
import os
def uploadKMZ(request):
if request.method == 'POST':
# Save the file updated
uploaded_file = request.FILES['document']
name = uploaded_file.name
fs = FileSystemStorage()
fs.save(uploaded_file.name, uploaded_file)
# Modify the extension (NOT WORKING)
thisFile = uploaded_file.name
name, ext = os.path.splitext(thisFile)
os.rename(thisFile, name + ".zip")
return render(request, 'data_app/kmzTemplate.html')
The error is FileNotFoundError
, why is not founding it if the file is the same but just changing the extension?
Thank you very much!
Upvotes: 0
Views: 291
Reputation: 2951
Did you try modifying your extension before saving the file?
I didn't try the code below, but if feels more sensible. Try it out.
from django.core.files.storage import FileSystemStorage
import os
def uploadKMZ(request):
if request.method == 'POST':
# Save the file updated
uploaded_file = request.FILES['document']
name, ext = os.path.splitext(uploaded_file.name)
new_name = name + '.zip'
fs = FileSystemStorage()
fs.save(new_name, uploaded_file)
return render(request, 'data_app/kmzTemplate.html')
Upvotes: 1