FileNotFoundError 尽管它位于正确的目录中

问题描述 投票:0回答:1

我想在vscode中用python打开一个文件,但是出现FileNotFoundError。 我的 txt 文件与 python 文件位于同一目录中。 我删除了该文件,然后将其再次写入正确的目录中,但没有帮助。

路径是正确的,并且当我使用 import os.path 时它可以工作。

python visual-studio-code path
1个回答
0
投票

您的工作目录似乎是 C:\Users\Raoul\Documents\Python\Visual Studio Code,但您的文件驻留在另一个目录中。

尝试将工作目录更改为 python 和 txt 文件所在的同一目录。

试试这个:

import os
os.chdir(r"C:\Users\Raoul\Documents\Python\Visual Studio Code\projects\mini_projects_9h")
© www.soinside.com 2019 - 2024. All rights reserved.