I want to write a pandas data frame to a CSV file. However, the last line of my code outputs following error.
'PRN' is the name of the ticker
{FileNotFoundError}[Errno 2] No such file or directory: G:\\stock_data/daily/PRN.csv
I already checked that the folder "G:\stock_data\daily" exists.
The issue seems that 'PRN' is a reserved name in windows for Printer. Is there a way that i can save a csv like PRN.csv? https://superuser.com/questions/613313/why-cant-we-make-con-prn-null-folder-in-windows
data = None
usbPath = 'G:\stock_data'
startDate = datetime.today() - timedelta(days=70)
try:
data = pdr.get_data_yahoo(tickers, start=startDate, progress=False, interval="1d")
except Exception as e:
pass
for ticker in tickers:
dic = {'Open': data['Open'][ticker], 'High': data['High'][ticker], 'Low': data['Low'][ticker], 'Close': data['Close'][ticker]}
df = pd.DataFrame(dic)
df.to_csv(os.path.abspath(usbPath) + '/daily/{}.csv'.format(ticker))
/instead of ` \ ` in the filename. This would work on Linux, but you appear to use Windows. Tryos.path.join(os.path.abspath(usbPath), 'daily', '{}.csv'.format(...)). It creates the path correctly on different operating systems