if sort_by:
    temp_path = '{0}/file.txt'.format(settings.SITE_ROOT)
new_text = 'custom'
f = open(temp_path, 'w')
f.write(new_text)
f.close()
handle = open(temp_path, 'w+')
handle.write(sort_by)
handle.close()
return HttpResponseRedirect(reverse('envelopes:editor'))
How to overwrite a file in Python?