解决 VSCode 调试时 Python 文件出现相对路径报错问题‘FileNotFoundError’

1. 问题描述

    在使用 VSCode 进行 Python 开发时,遇到一个的问题:在调试模式下,程序无法读取文件或路径,导致File Not Found Error 错误。然而,当不使用调试模式而是直接运行 Python 文件时,一切又正常工作。本文将详细探讨这个问题,并给出解决方法。

2. 解决方法

在工程项目根目录下创建.vscode文件夹,并在这个文件夹下创建launch.json文件,具体目录结构如下:
在这里插入图片描述

launch.jsion内容如下,只需修改红色方框内的两个参数,其中${workspaceFolder}表示工程项目的根目录。

在这里插入图片描述

代码如下:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Debug",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/code/ACDC_BCP_train.py",  // 你的主程序文件
            "cwd": "${workspaceFolder}/code",  // 将工作目录设置为 code 文件夹
            "console": "integratedTerminal",
            "justMyCode": true
        }
    ]
}
报错:PS C:\Gerrit\Leopard_Code\R33> & C:/Users/lang.sun/AppData/Local/Programs/Python/Python313/python.exe c:/Users/lang.sun/Desktop/213/1.py Traceback (most recent call last): File "c:\Users\lang.sun\Desktop\213\1.py", line 39, in <module> filtered_df, target_rows = filter_data() ~~~~~~~~~~~^^ File "c:\Users\lang.sun\Desktop\213\1.py", line 12, in filter_data df = pd.read_excel(src_path, engine='openpyxl') File "C:\Users\lang.sun\AppData\Local\Programs\Python\Python313\Lib\site-packages\pandas\io\excel\_base.py", line 495, in read_excel io = ExcelFile( io, ...<2 lines>... engine_kwargs=engine_kwargs, ) File "C:\Users\lang.sun\AppData\Local\Programs\Python\Python313\Lib\site-packages\pandas\io\excel\_base.py", line 1567, in __init__ self._reader = self._engines[engine]( ~~~~~~~~~~~~~~~~~~~~~^ self._io, ^^^^^^^^^ storage_options=storage_options, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ engine_kwargs=engine_kwargs, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "C:\Users\lang.sun\AppData\Local\Programs\Python\Python313\Lib\site-packages\pandas\io\excel\_openpyxl.py", line 553, in __init__ super().__init__( ~~~~~~~~~~~~~~~~^ filepath_or_buffer, ^^^^^^^^^^^^^^^^^^^ storage_options=storage_options, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ engine_kwargs=engine_kwargs, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "C:\Users\lang.sun\AppData\Local\Programs\Python\Python313\Lib\site-packages\pandas\io\excel\_base.py", line 563, in __init__ self.handles = get_handle( ~~~~~~~~~~^ filepath_or_buffer, "rb", storage_options=storage_options, is_text=False ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "C:\Users\lang.sun\AppData\Local\Programs\Python\Python313\Lib\site-packages\pandas\io\common.py", line 882, in get_handle handle = open(handle, ioargs.mode) FileNotFoundError: [Errno 2] No such file or directory: 'HPC_CS対応結果_V0.22 - 副本 1.xlsx' PS C:\Gerrit\Leopard_Code\R33>
最新发布
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cpdr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值