I have a script to download a PDF from a URL working, but it saves it to the same directory the python script is in. I would like to save it to another directory
I've tried changing this with save_path
however it isn't working, but I also do not get any errors
# Import all needed modules and tools
from fileinput import filename
import os
import os.path
from datetime import datetime
import urllib.request
import requests
# Disable SSL and HTTPS Certificate Warnings
import urllib3
urllib3.disable_warnings()
resp = requests.get('url.pdf', verify=False)
response= urllib.request.urlretrieve('url.pdf',
filename = 'civil.pdf')
save_path = "C: my preferred directory"
CodePudding user response:
Simply you can use it directly with filename
urllib.request.urlretrieve('url.pdf',
filename='C:\\you_preferred directory\\civil.pdf')
And if you want download with requests
with open("C:\\you_preferred directory\\civil.pdf", 'wb') as f:
f.write(resp.content)
Eventually you should change current directory before downloading
os.chdir("C:\\you_preferred directory\\")
and then you can use short filename='civil.pdf'
and open('civil.pdf', 'wb')
CodePudding user response:
Solved, per comments
# Import all needed modules and tools
from fileinput import filename
import os
import os.path
from datetime import datetime
import urllib.request
import requests
# Disable SSL and HTTPS Certificate Warnings
import urllib3
urllib3.disable_warnings()
resp = requests.get('url.org', verify=False)
# Download and name the PDF file from the URL
response= urllib.request.urlretrieve('url.pdf',
filename = 'C:\\ location')
# Save to the preferred directory
with open("C:\\ location", 'wb') as f: f.write(resp.content)