Computer for Everybody

Process File

Random String

from random import choices
def random_string(n):
    pattern = '1234567890qwertyuiopasdfghjklzxcvbnm'
    return ''.join(choices(pattern, k = n))

Random Filename

from os.path import splitext
def random_filename(n, filename):
    _, ext = splitext(filename)
    return random_string(n - len(ext)) + ext

Upload file

def upload(f):
    filename = random_filename(32, f.name)
    with open('./statics/images/{filename}') as desc:
        for chunk in f.chunks():
            des.write(chunk)
    return filename