logo
Loading...

Day6 os.path.join 設置問題 - Cupoy

爬了之前的回答,將application_train.csv檔案放入ipynb檔相同的資料夾底下,但...

ml_002,ml_006,os.path

Day6 os.path.join 設置問題

2020/07/27 09:26 PM
機器學習新手論壇
Ethan
觀看數:0
回答數:2
收藏數:3
ml_002
ml_006
os.path

爬了之前的回答,將application_train.csv檔案放入ipynb檔相同的資料夾底下,但是仍然無法成功讀取到csv檔案。


只有直接利用下面一程式碼才能成功讀取csv檔

app_train = pd.read_csv('application_train.csv')


以下為錯誤訊息:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-48-cba3a2d69f89> in <module>
     3 dir_data = 'ML_marathon/Day_006'
     4 f_app_train = os.path.join(dir_data, 'application_train.csv')
----> 5 app_train = pd.read_csv(f_app_train)
     6
     7 # data_folder = os.path.join('ML_marathon', 'Day_006')

D:\Anaconda_JupyterNotebook\lib\site-packages\pandas\io\parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, dialect, error_bad_lines, warn_bad_lines, delim_whitespace, low_memory, memory_map, float_precision)
   674         )
   675
--> 676         return _read(filepath_or_buffer, kwds)
   677
   678     parser_f.__name__ = name

D:\Anaconda_JupyterNotebook\lib\site-packages\pandas\io\parsers.py in _read(filepath_or_buffer, kwds)
   446
   447     # Create the parser.
--> 448     parser = TextFileReader(fp_or_buf, **kwds)
   449
   450     if chunksize or iterator:

D:\Anaconda_JupyterNotebook\lib\site-packages\pandas\io\parsers.py in __init__(self, f, engine, **kwds)
   878             self.options["has_index_names"] = kwds["has_index_names"]
   879
--> 880         self._make_engine(self.engine)
   881
   882     def close(self):

D:\Anaconda_JupyterNotebook\lib\site-packages\pandas\io\parsers.py in _make_engine(self, engine)
  1112     def _make_engine(self, engine="c"):
  1113         if engine == "c":
-> 1114             self._engine = CParserWrapper(self.f, **self.options)
  1115         else:
  1116             if engine == "python":

D:\Anaconda_JupyterNotebook\lib\site-packages\pandas\io\parsers.py in __init__(self, src, **kwds)
  1889         kwds["usecols"] = self.usecols
  1890
-> 1891         self._reader = parsers.TextReader(src, **kwds)
  1892         self.unnamed_cols = self._reader.unnamed_cols
  1893

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader.__cinit__()

pandas\_libs\parsers.pyx in pandas._libs.parsers.TextReader._setup_parser_source()

FileNotFoundError: [Errno 2] File ML_marathon/Day_006\application_train.csv does not exist: 'ML_marathon/Day_006\\application_train.csv'

--------------------------------------------------------------------------- 

嘗試利用下面這一篇文章來解決問題,同樣出現錯誤

 [How to set the os.path](https://medium.com/@ageitgey/python-3-quick-tip-the-easy-way-to-deal-with-file-paths-on-windows-mac-and-linux-11a072b58d5f#:~:text=To%20use%20it%2C%20you%20just,for%20the%20current%20operating%20system.


<Method 1>

dir_data = 'ML_marathon/Day_006'

f_app_train = os.path.join(dir_data, 'application_train.csv')

app_train = pd.read_csv(f_app_train)


<Method 1>

data_folder = os.path.join('ML_marathon', 'Day_006')

file_to_open = os.path.join(data_folder, r'application_train.csv')

app_train = pd.read_csv(file_to_open)



Q1: 想請問我這樣設置os.path有錯誤嗎? 

Q2: 我可以如何修改我的程式碼來建立os.path?


謝謝