0

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))


3
  • 2
    I think you might have a problem in the directions of the slashes Commented Aug 27, 2019 at 11:44
  • 2
    You used / instead of ` \ ` in the filename. This would work on Linux, but you appear to use Windows. Try os.path.join(os.path.abspath(usbPath), 'daily', '{}.csv'.format(...)). It creates the path correctly on different operating systems Commented Aug 27, 2019 at 11:45
  • take a look at the filepath G:\\stock_data/daily/PRN.csv . Seems to me that your slashes got mixed up Commented Aug 27, 2019 at 11:46

2 Answers 2

1

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)))
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.