I have this script which is to pull stock prices for the last so many years from yahoo finance.
Once the code is run, i am not able to write again on the file even though it is suppose to check what exists and append.
I get the following error: FileExistsError: [Errno 17] File exists: 'stocks_dfs'
Please advise!
'''python
import bs4 as bs
import pickle
import requests
import datetime as dt
import pandas as pd
import os
import pandas_datareader.data as web
from time import sleep
def save_sp500_tickers():
resp = requests.get('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
soup =bs.BeautifulSoup(resp.text,'lxml')
table=soup.find('table',{'class':'wikitable sortable'})
tickers=[]
for row in table.findAll('tr')[1:]:
ticker= row.findAll('td')[1].text
tickers.append(ticker)
with open("sp500ticker.pickle", "wb") as f:
pickle.dump(tickers,f)
print (tickers)
return tickers
#save_sp500_tickers()
def get_data_from_yahoo(reload_sp500=False):
if reload_sp500:
tickers=save_sp500_tickers()
else:
with open("sp500ticker.pickle","rb") as f:
tickers=pickle.load(f)
if not os.path.exists('stock_dfs'):
os.makedirs('stocks_dfs')
start = dt.datetime(2016,1,1)
end = dt.datetime.now()
for ticker in tickers:
print(ticker)
if not os.path.exists('stocks_dfs/{}.csv'.format(ticker)):
df = web.DataReader(ticker,'yahoo', start, end)
df.to_csv('stocks_dfs/{}.csv'.format(ticker))
else:
print('Already have {}'.format(ticker))
get_data_from_yahoo()
'''
FileExistsError: [Errno 17] File exists: 'stocks_dfs'
You are complaining that this line fails:
os.makedirs('stocks_dfs')
To support repeated invocations, you'll want to specify the exist_ok=True
flag.