前提条件
PythonのPandasを用いて、Macのデスクトップに保存してある[data.xlsx]というExcelファイルをjupyter labで動作させたいのですが、FileNotFoundErrorが起きます。
環境としては、
Python 3.10.2
jupyterlab 3.3.4
pandas 1.4.2
xlrd 1.2.0
*anacondaはインストールしていません。
対処法を調べましたがわからなかったため質問です。
具体的には、
1・xlrdが2.0以降xlsxファイルに対応しないと知ったため、1.2.0に変更しましたが効果はありませんでした。
2・openpyxlを指定してファイルを読み取る。
Python
1import pandas as pd 2data_list = pd.read_excel('data.xlsx',engine = "openpyxl")
openpyxl 3.0.9 がインストール済みであることを確認しましたが、これも効果はありませんでした。
3・xlsxファイルをcsvファイルに変換してread_csv()関数を使う。これも効果なし。
実行したコード
Python
1import pandas as pd 2data_list = pd.read_excel('data.xlsx')
エラーコード
Python
1--------------------------------------------------------------------------- 2FileNotFoundError Traceback (most recent call last) 3Input In [11], in <cell line: 2>() 4 1 import pandas as pd 5----> 2 data_list = pd.read_excel('data.xlsx') 6 7File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/util/_decorators.py:311, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs) 8 305 if len(args) > num_allow_args: 9 306 warnings.warn( 10 307 msg.format(arguments=arguments), 11 308 FutureWarning, 12 309 stacklevel=stacklevel, 13 310 ) 14--> 311 return func(*args, **kwargs) 15 16File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/io/excel/_base.py:457, in read_excel(io, sheet_name, header, names, index_col, usecols, squeeze, dtype, engine, converters, true_values, false_values, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, parse_dates, date_parser, thousands, decimal, comment, skipfooter, convert_float, mangle_dupe_cols, storage_options) 17 455 if not isinstance(io, ExcelFile): 18 456 should_close = True 19--> 457 io = ExcelFile(io, storage_options=storage_options, engine=engine) 20 458 elif engine and engine != io.engine: 21 459 raise ValueError( 22 460 "Engine should not be specified when passing " 23 461 "an ExcelFile - ExcelFile already has the engine set" 24 462 ) 25 26File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/io/excel/_base.py:1376, in ExcelFile.__init__(self, path_or_buffer, engine, storage_options) 27 1374 ext = "xls" 28 1375 else: 29-> 1376 ext = inspect_excel_format( 30 1377 content_or_path=path_or_buffer, storage_options=storage_options 31 1378 ) 32 1379 if ext is None: 33 1380 raise ValueError( 34 1381 "Excel file format cannot be determined, you must specify " 35 1382 "an engine manually." 36 1383 ) 37 38File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/io/excel/_base.py:1250, in inspect_excel_format(content_or_path, storage_options) 39 1247 if isinstance(content_or_path, bytes): 40 1248 content_or_path = BytesIO(content_or_path) 41-> 1250 with get_handle( 42 1251 content_or_path, "rb", storage_options=storage_options, is_text=False 43 1252 ) as handle: 44 1253 stream = handle.handle 45 1254 stream.seek(0) 46 47File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/io/common.py:798, in get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options) 48 789 handle = open( 49 790 handle, 50 791 ioargs.mode, 51 (...) 52 794 newline="", 53 795 ) 54 796 else: 55 797 # Binary mode 56--> 798 handle = open(handle, ioargs.mode) 57 799 handles.append(handle) 58 801 # Convert BytesIO or file objects passed with an encoding 59 60FileNotFoundError: [Errno 2] No such file or directory: 'data.xlsx' 61
最後に
まだプログラミングを始めて間もないため、詳しく教えていただける幸いです。
ご回答よろしくお願いいたします。
あなたの回答
tips
プレビュー