太子丶
愿你走出半生,归来仍为少年。

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}  当前打开的文件,光标所在的行数
posted on 2020-12-23 10:34  太子丶  阅读(3714)  评论(0编辑  收藏  举报