vscode中open无法找到文件的解决办法

1 解决方法

在python开发工程目录下有一个".vscode"文件夹,在该文件夹下有一个launch.json文件,在 launch.json中添加一行cwd的信息,就可以解决open找不到文件的问题。

 

添加行如下所示:

"cwd": "${fileDirname}",

 

完整的launch.json如下所示:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        
        {
            "name": "Python: 当前文件",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "cwd": "${fileDirname}",
        }
    ]
}

2 预定义变量介绍

vscode预定义变量介绍,如下所示

Predefined variables

The following predefined variables are supported:

  • ${workspaceFolder} - the path of the folder opened in VS Code

  • ${workspaceFolderBasename} - the name of the folder opened in VS Code without any slashes (/)

  • ${file} - the current opened file

  • ${relativeFile} - the current opened file relative to workspaceFolder

  • ${relativeFileDirname} - the current opened file's dirname relative to workspaceFolder

  • ${fileBasename} - the current opened file's basename

  • ${fileBasenameNoExtension} - the current opened file's basename with no file extension

  • ${fileDirname} - the current opened file's dirname

  • ${fileExtname} - the current opened file's extension

  • ${cwd} - the task runner's current working directory on startup

  • ${lineNumber} - the current selected line number in the active file

  • ${selectedText} - the current selected text in the active file

  • ${execPath} - the path to the running VS Code executable

  • ${defaultBuildTask} - the name of the default build task

 

vscode预定义变量介绍,相关中文解释:

  • ${workspaceRoot} 当前打开的文件夹的绝对路径+文件夹的名字
  • ${workspaceRootFolderName}   当前打开的文件夹的名字
  • ${file} 当前打开正在编辑的文件名,包括绝对路径,文件名,文件后缀名
  • ${relativeFile} 从当前打开的文件夹到当前打开的文件的路径
如当前打开的是test文件夹,当前的打开的是main.c,并有test/A/B/main.c,那么此变量代表的是A/B/main.c
 
  • ${fileBasename}  当前打开的文件名+后缀名,不包括路径
  • ${fileBasenameNoExtension} 当前打开的文件的文件名,不包括路径和后缀名
  • ${fileDirname} 当前打开的文件所在的绝对路径,不包括文件名
  • ${fileExtname} 当前打开的文件的后缀名
  • ${cwd} 当前运行的文件所在目录
  • ${lineNumber}  当前打开的文件,光标所在的行数

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值