Reputation: 75
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))
Upvotes: 0
Views: 780
Reputation: 82755
Use os.path.join
Ex:
import os
usbPath = r'G:\stock_data' #Note r in front of windows path
df.to_csv(os.path.join(usbPath, "daily", '{}.csv'.format(ticker)))
Upvotes: 1
Reputation: 6260
It is the most common mistake regarding this topic, it is a syntax error, if you change it to:
usbPath= 'G:/stock_data'
it will work
Upvotes: 0