import os def write_file(file, file_dir): if not os.path.exists(file_dir): os.makedirs(file_dir) file_path = file_dir + "/" + file.name with open(file_path, 'wb') as f: f.write(file.read()) f.close()