简体   繁体   English

Python os.getcwd() 不适用于 VSCODE 中的子文件夹

[英]Python os.getcwd() is not working on subfolders in VSCODE

I have a python file, converted from a Jupiter Notebook, and there is a subfolder called 'datasets' inside this file folder.我有一个 python 文件,它是从 Jupiter Notebook 转换而来的,并且该文件夹中有一个名为“datasets”的子文件夹。 When I'm trying to open a file that is inside that 'datasets' folder, with this code:当我尝试使用以下代码打开该“数据集”文件夹内的文件时:

import pandas as pd
# Load the CSV data into DataFrames
super_bowls = pd.read_csv('/datasets/super_bowls.csv')

It says that there is no such file or folder.它说没有这样的文件或文件夹。 Then I add this line然后我添加这一行

os.getcwd()

And the output is the top-level folder of the project, and not the subfolder when is this python file. output 是项目的顶级文件夹,而不是 python 文件时的子文件夹。 And I think maybe that's the reason why it's not working.我想也许这就是它不起作用的原因。

So, how can I open that csv file with relative paths?那么,如何使用相对路径打开该 csv 文件? I don't want to use absolute path because this code is going to be used in another computers.我不想使用绝对路径,因为此代码将在另一台计算机中使用。

Why os.getcwd() is not getting the actual folder path?为什么 os.getcwd() 没有得到实际的文件夹路径?

(edited) (已编辑)

Per your comment below, the current working directory is根据您在下面的评论,当前工作目录是

/Users/ivanparra/AprendizajePython/

while the file is in当文件在

/Users/ivanparra/AprendizajePython/Jupyter/datasets/super_bowls.csv

For that reason, going to the datasets subfolder of the current working directory (CWD) takes you to /Users/ivanparra/AprendizajePython/datasets which either doesn't exist or doesn't contain the file you're looking for.出于这个原因,转到当前工作目录 (CWD) 的datasets子文件夹会将您带到/Users/ivanparra/AprendizajePython/datasets不存在或不包含您要查找的文件。

You can do one of two things:您可以执行以下两项操作之一:

(1) Use an absolute path to the file, as in (1) 使用文件的绝对路径,如

super_bowls = pd.read_csv("/Users/ivanparra/AprendizajePython/Jupyter/datasets/super_bowls.csv")

(2) use the right relative path, as in (2) 使用正确的相对路径,如

super_bowls = pd.read_csv("./Jupyter/datasets/super_bowls.csv")

There's also (3) - use os.path.join to contact the CWD to the relative path - it's basically the same as (2).还有 (3) - 使用os.path.join将 CWD 联系到相对路径 - 它与 (2) 基本相同。 (you can also use (你也可以使用

My observation, the dot (.) notation to move to the parent directory sometimes does not work depending on the operating system.根据我的观察,移动到父目录的点 (.) 符号有时不起作用,具体取决于操作系统。 What I generally do to make it os agnostic is this:我通常做的是使它与操作系统无关:

import pandas as pd
import os

__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))

super_bowls = pd.read_csv(__location__ + '/datasets/super_bowls.csv')

This works on my windows and ubantu machine equally well.这在我的 windows 和 ubantu 机器上同样适用。 I am not sure if there are other and better ways to achieve this.我不确定是否有其他更好的方法来实现这一目标。 Would like to hear back if there are.如果有的话,想听听。

Try this code试试这个代码

super_bowls = pd.read_csv( os.getcwd() + '/datasets/super_bowls.csv')

Thanks to everyone that tried to help me.感谢所有试图帮助我的人。 Thanks to the Roy2012 response, I got a code that works for me.感谢 Roy2012 的回复,我得到了一个适合我的代码。

import pandas as pd
import os

currentPath = os.path.dirname(__file__)

# Load the CSV data into DataFrames
super_bowls = pd.read_csv(currentPath + '/datasets/super_bowls.csv')

The os.path.dirname gives me the path of the current file, and let me work with relative paths. os.path.dirname 给了我当前文件的路径,让我使用相对路径。

'/Users/ivanparra/AprendizajePython/Jupyter'

and with that it works like a charm!!有了它,它就像一个魅力!

PS: As a side note, the behavior of os.getcwd() is quite different in a Jupyter Notebook than a python file. PS:作为旁注, os.getcwd() 的行为在 Jupyter Notebook 中与 python 文件完全不同。 Inside the notebook, that function gives the current file path, but in a python file, gives the top folder path.在笔记本中,function 给出了当前文件路径,但在 python 文件中,给出了顶部文件夹路径。

I noticed this problem a few years ago.几年前我注意到了这个问题。 I think it's a matter of design style.我认为这是设计风格的问题。 The problem is that: your workspace folder is just a folder, not a project folder.问题在于:您的工作区文件夹只是一个文件夹,而不是项目文件夹。 Most of the time, your relative reference is based on the current file.大多数情况下,您的相对参考基于当前文件。

VSCode actually supports the dynamic setting of cwd, but that's not the default. VSCode 实际上支持 cwd 的动态设置,但这不是默认的。 If your work folder is not a rigorous and professional project, I recommend you adding the following settings to launch.json .如果您的工作文件夹不是一个严谨专业的项目,我建议您在launch.json中添加以下设置。 This is the simplest answer you need.这是您需要的最简单的答案。

"cwd": "${fileDirname}"

The answer really lies in the response by user2357112: os.getcwd() is working fine.答案确实在于 user2357112 的响应: os.getcwd() 工作正常。 The problem is in your expectations.问题在于你的期望。 The current working directory is the directory where Python is running, not the directory of any particular source file.当前工作目录是 Python 正在运行的目录,而不是任何特定源文件的目录。 – user2357112 supports Monica May 22 at 6:03 – user2357112 支持 Monica 5 月 22 日 6:03

The solution is:解决方案是:

    data_dir = os.path.dirname(__file__)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM