Found a bug of a 500 error that happens sometimes in the sandbox. The error might be because of creating the files in the static folders. Changed the script to create the files in a temporary location then moving to a different location.