0

This code is supposed to download a list of pdfs into a directory

for pdf in preTag:
    pdfUrl = "https://the-eye.eu/public/Books/Programming/" + 
    pdf.get("href")
    print("Downloading...%s"% pdfUrl)
    #downloading pdf from url
    page = requests.get(pdfUrl)
    page.raise_for_status()

    #saving pdf to new directory
    pdfFile = open(os.path.join(filePath, os.path.basename(pdfUrl)), "wb")
    for chunk in page.iter_content(1000000):
        pdfFile.write(chunk)
pdfFile.close()

I used os.path.basename() just to make sure the files would actually download. However, I want to know how to change the file name from 3D%20Printing%20Blueprints%20%5BeBook%5D.pdf to something like "3D Printing Blueprints.pdf"

1
  • Hello, i think you should edit your question. If you want an information about manipulating strings or changing the name of a file, we really do not need to know all your code. Please take one minute to read this : How to create a Minimal, Complete, and Verifiable example Commented Apr 24, 2018 at 22:11

2 Answers 2

1

You can use the urllib2 unquote function:

import urllib2
print urllib2.unquote("3D%20Printing%20Blueprints%20%5BeBook%5D.pdf") #3D Printing Blueprints.pdf
Sign up to request clarification or add additional context in comments.

Comments

0

use this:

os.rename("3D%20Printing%20Blueprints%20%5BeBook%5D.pdf", "3D Printing Blueprints.pdf")

you can find more info here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.