You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS Code读取同目录Excel文件遇FileNotFoundError但IDLE可正常运行

问题:VS Code读取同目录Excel文件报错,Python IDLE可正常运行

我需要读取同目录下的book.xlsx文件开发程序,但在VS Code中运行代码时始终出现以下错误:

Traceback (most recent call last):
File "c:\Users\psypa\Documents\python\dev python\CPI\cpitest1.py", line 3, in <module>
dataframe1 = pandas.read_excel('book.xlsx',engine='openpyxl')
File "C:\Users\psypa\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel_base.py", line 478, in read_excel
io = ExcelFile(io, storage_options=storage_options, engine=engine)
File "C:\Users\psypa\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel_base.py", line 1513, in __init__
self._reader = self._engines[engine](self._io, storage_options=storage_options)
File "C:\Users\psypa\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel_openpyxl.py", line 549, in __init__
super().__init__(filepath_or_buffer, storage_options=storage_options)
File "C:\Users\psypa\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel_base.py", line 530, in __init__
self.handles = get_handle(
File "C:\Users\psypa\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\common.py", line 868, in get_handle
handle = open(handle, ioargs.mode)
FileNotFoundError: [Errno 2] No such file or directory: 'book.xlsx'

但相同代码在Python IDLE中可正常运行。

原代码:

import pandas 

dataframe1 = pandas.read_excel('book.xlsx')

print(dataframe1)

替代版本:

import pandas

dataframe1 = pandas.read_excel('book.xlsx',engine='openpyxl')

print(dataframe1)

解决方法

1. 确认VS Code的工作目录

VS Code默认运行代码的工作目录可能不是脚本所在文件夹。可以在代码里添加以下内容,打印当前工作路径验证:

import os
print(os.getcwd())

如果输出路径不是c:\Users\psypa\Documents\python\dev python\CPI\,说明工作目录不匹配。

2. 修改VS Code运行时的工作目录

  • 方法一:开启自动切换目录
    打开VS Code设置(快捷键Ctrl+,),搜索python.terminal.executeInFileDir,勾选该选项。之后运行代码时,终端会自动切换到脚本所在目录。
  • 方法二:通过launch.json配置
    点击左侧“运行和调试”面板,创建launch.json文件,在配置中添加"cwd": "${fileDirname}",示例如下:
    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Python: 当前文件",
                "type": "python",
                "request": "launch",
                "program": "${file}",
                "console": "integratedTerminal",
                "cwd": "${fileDirname}"
            }
        ]
    }
    

3. 使用绝对路径读取文件

如果不想修改工作目录,可直接使用文件绝对路径,或动态生成路径避免硬编码:

  • 硬编码绝对路径:
    import pandas
    dataframe1 = pandas.read_excel(r'c:\Users\psypa\Documents\python\dev python\CPI\book.xlsx')
    print(dataframe1)
    
  • 动态生成路径(推荐):
    import pandas
    import os
    # 获取脚本所在目录
    script_dir = os.path.dirname(os.path.abspath(__file__))
    # 拼接Excel文件路径
    file_path = os.path.join(script_dir, 'book.xlsx')
    dataframe1 = pandas.read_excel(file_path)
    print(dataframe1)
    

内容的提问来源于stack exchange,提问作者psypad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 15:55:33