简体   繁体   English

Python 文件始终运行在 C:/Users/User 而不是工作目录中

[英]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.pyD:\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.

相关问题 python代码需要获取用户的用户名才能打开C:/Users/USERNAME/目录下的文件 - The python code needs to get the user's username to open a file on the C:/Users/USERNAME/ directory PyCharm:“ bash:/Users/user/.bashrc:没有此类文件或目录” - PyCharm: “bash: /Users/user/.bashrc: No such file or directory” howoto修复工作目录始终位于主目录吗? (蟒蛇) - howoto fix working directory is always home? (python) 将目录更改为“C:\Users\<username> ” 与 Python 操作系统</username> - Change the directory to “C:\Users\<username>” with Python OS 当 python 脚本与 mlflow 一起运行时,工作目录更改为 /tmp/ - working directory changes to /tmp/ when python script runs with mlflow Python:FileNotFoundError:[Errno 2] 没有这样的文件或目录:'C:\\Users\\Aditya Bhatt\\Desktop\\Aditya\\SampleData.csv' - Python: FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Aditya Bhatt\\Desktop\\Aditya\\SampleData.csv' FileNotFoundError: [Errno 2] 没有这样的文件或目录:'C:\\Users\\SUN\\Desktop\\oops in python.txt' - FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\SUN\\Desktop\\oops in python.txt' Python 3 中的 Else 语句始终运行 - Else statement in Python 3 always runs os.mkdir 成功但 C:\Users\ 中没有目录出现<user></user> - os.mkdir succeeds but no directory appears in C:\Users\<user> 我是否总是需要在 Python 中指定我当前的工作目录? - Do I always need to specify my current working directory in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM