title: Python File Upload rename, Zip and save
body: <p>My requirement is</p>
  
  <ol>
  <li>File upload, </li>
  <li>Rename the file,</li>
  <li>Compress it - make it as a zip, </li>
  <li>Save in required location,</li>
  <li>unzip the zip file,</li>
  <li>Remove the zip file,</li>
  <li>save the renamed file and filesize in DB</li>
  </ol>
  
  <p>Renaming and zipping the file is not happening. Let me add my code here </p>
  
  <pre><code>@app.route('/temp/upload', methods=['POST'])
  def upload():
      if request.method == 'POST':
          f = request.files['file']
  
          random_string = generate_random(5)
          print "Random string=&gt; ", random_string # wqzhp
          print "f.filename-&gt;&gt;&gt; ", f.filename  # form3.pdf
  
          fn = f.filename
          fname = (fn).split(".")
          random_file_name = fname[0] + '-' + random_string + "." + fname[1]
          print "random_file_name&gt;&gt; ", random_file_name # form3-wqzhp.pdf
  
          actual_file = os.path.join(load_path, random_file_name)
          print "Actual file -&gt; ", actual_file # uploadedDocs/1/form3-wqzhp.pdf
  
          zip_f_name = actual_file + ".zip"
          print "zip_f_name--&gt; ", zip_f_name # uploadedDocs/1/form3-wqzhp.pdf.zip
  
          f.save(zip_f_name)
          zip_ref = zipfile.ZipFile(zip_f_name, 'r')
          zip_ref.extractall(load_path)
          zip_ref.close()
          os.remove(zip_f_name)
  
          if update_file_status(random_file_name, 0, csvid) is False:
              return "update failed"
          return "success"
  </code></pre>
  
  <p>When the zip is extracted, only original file uploaded is unzipped.  zipping with the renamed file is not happening! How to save the renamed file in zip?</p>
  
  <p>Any Clues!</p>