[英]Python file always runs at C:/Users/User and not in the working directory
When I'm launching python file in VS Code that for example located at C:\Users\andre\Desktop\NewFolder\Something.py
or D:\Pythons\Something.py
当我在 VS Code 中启动 python 文件时,例如位于
C:\Users\andre\Desktop\NewFolder\Something.py
或D:\Pythons\Something.py
I'm getting C:\Users\andre
via os.getcwd()
and not the directory where python script is located.我通过
os.getcwd()
获取C:\Users\andre
而不是 python 脚本所在的目录。
VS Code takes the open folder as the workspace. VS Code 将打开的文件夹作为工作区。 So please open the whole folder in VS Code, not just a
.py
file, then VS Code will not be able to find your workspace and will use the computer user directory as the workspace by default.所以请在 VS Code 中打开整个文件夹,而不仅仅是一个
.py
文件,那么 VS Code 将无法找到您的工作区,并且默认使用计算机用户目录作为工作区。 So naturally you get the wrong script path.所以很自然地你会得到错误的脚本路径。
Example:例子:
Open the folder as a workspace:将文件夹作为工作区打开:
Just open a .py file:只需打开一个 .py 文件:
VS Code open folde: VS Code 打开文件夹:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.