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} 從當前打開的文件夾到當前打開的文件的路徑
-
${fileBasename} 當前打開的文件名+后綴名,不包括路徑
-
${fileBasenameNoExtension} 當前打開的文件的文件名,不包括路徑和后綴名
-
${fileDirname} 當前打開的文件所在的絕對路徑,不包括文件名
-
${fileExtname} 當前打開的文件的后綴名
-
${cwd} 當前運行的文件所在目錄
-
${lineNumber} 當前打開的文件,光標所在的行數