'How can I solve FileNotFound Error on Jupyter Notebook, Python3

define time interval

def read_yahoo_data(fname):
   pd.read_csv(fname, index_col=0, na_values="null").dropna()

get pandas data for Japanese and American stocks

toyota     = read_yahoo_data('/Users/chenyuhan/Downloads/yahoo_finance/TM.csv')

mitsui     = read_yahoo_data('/Users/chenyuhan/Downloads/yahoo_finance/MTU.csv')

mitsubishi = read_yahoo_data('/Users/chenyuhan/Downloads/yahoo_finance/MITSY.csv')

apple      = read_yahoo_data('/Users/chenyuhan/Downloads/yahoo_finance/APPL.csv')

msft       = read_yahoo_data('/Users/chenyuhan/Downloads/yahoo_finance/MSFT.csv')

hpq        = read_yahoo_data('/Users/chenyuhan/Downloads/yahoo_finance/HPQ.csv')

nikkei     = read_yahoo_data('/Users/chenyuhan/Downloads/yahoo_finance/^N225.csv')

sp500      = read_yahoo_data('/Users/chenyuhan/Downloads/yahoo_finance/^GSPC.csv')

This is the code that I used and the path for files should be true. I am using a MacOS.



Solution 1:[1]

pandas csv_read starts from the working directory. So you have two ways to achieve this:

  1. pass relative path(e.g. "../../../Users/xxxx/xxx.csv")
  2. change working directory.

With option 2, your code would be something like this:

def read_yahoo_data(fname):
    import pandas as pd
    import os
    os.chdir('/Users/chenyuhan/Downloads/yahoo_finance/')
    return pd.read_csv(fname, index_col=0, na_values="null").dropna()

And you need to pass the filename like this:

toyota     = read_yahoo_data('TM.csv')
mitsui     = read_yahoo_data('MTU.csv')
mitsubishi = read_yahoo_data('MITSY.csv')
apple      = read_yahoo_data('APPL.csv')
msft       = read_yahoo_data('MSFT.csv')
hpq        = read_yahoo_data('HPQ.csv')
nikkei     = read_yahoo_data('^N225.csv')
sp500      = read_yahoo_data('^GSPC.csv')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 shogo2022