问题描述
所以我在可视化代码中遇到了这个问题,当我在一个项目的同一目录中打开一个文件时,除非我给他整个目录,否则可视化代码不会识别该文件
这是错误的图像:
解决方法
试试这样手动设置工作目录:
/tmp/1/
/tmp/2/file
import os
print("Current working directory: {0}".format(os.getcwd()))
> Current working directory: /tmp/1
open('file','r')
> Traceback (most recent call last):
> File "<stdin>",line 1,in <module>
> FileNotFoundError: [Errno 2] No such file or directory: 'file'
os.chdir('/tmp/2')
open('file','r')
> <_io.TextIOWrapper name='file' mode='r' encoding='UTF-8'>
您也可以设置与正在运行的python文件相关的目录:
import os
print(os.path.dirname(os.path.abspath(__file__)))
python /tmp/1/file.py
> /tmp/1
好的做法是在全局配置文件中的某处设置应用程序路径
,cwd:
指定调试器的当前工作目录,它是代码中使用的任何相对路径的基本文件夹。如果省略,则默认为 ${workspaceFolder}(在 VS Code 中打开的文件夹)。
您可以将其设置为:${fileDirname}
- 当前打开的文件的目录名(official docs)