Reputation: 137
I am trying to save get request data in csv format using python, the objective is to save the data in csv in a defined folder with filename as "abc_soi_today's date-1". for example
case 1: if i run the code today i.e. 15th july 2021 then the file name should be "abc_soi_20210714"
import urllib.request as req
from datetime import datetime, timedelta
def download_csv(download_url):
request = req.urlopen(download_url)
yesterday = datetime.date().today().isoformat().replace("-", "") - timedelta(1)
filename = yesterday.strftime('%Y%m%d')
file = open("C:\\Users\\atul.sanwal\\Desktop\\" + filename + '.csv', 'wb')
file.write(request.read())
file.close()
download_csv("http://uspvalpc064:8080/python-toolkit/natwest/file?source=dataDelivery&type=soi&destination=pv&asof=20210714")
Upvotes: 0
Views: 65
Reputation: 13661
Change it to:
from datetime import datetime, timedelta
yesterday = datetime.today().date() - timedelta(days=1)
filename = yesterday.strftime('%Y%m%d')
print(filename)
Output:
20210716
Upvotes: 1