具有多种文件类型和协作者的相对路径

发布于 2025-01-13 19:42:20 字数 1079 浏览 4 评论 0原文

当我想要访问同级文件夹中的 pickle 数据文件时,我无法使用相同的(相对)路径。因为我与多个协作者一起工作,这导致每次 git push/pull 后都必须更改 file_path 变量(参见下面的代码片段),这很烦人,而且可能是不必要的。

文件结构如下所示:

workspace_folder
    -data
        -.raw
            -.-data.pickle
    -src
        -.data_analyis.py
        -.rapid_experimentation.ipynb

我的合作者在 data_analysis.py 中编写了可在他的电脑上运行的以下代码:

# '..' to access sibling folder
file_path = r"../data/raw/data.pickle"

# read file
with open(file_path, 'rb') as f:
    data = pickle.load(f)

这给了我一个文件未找到错误,因为 VSCode 假定根文件夹是workspace_folder ,而不是 data_analysis.py 的位置。所以我必须使用:

# start from work_space folder, so no '..'
file_path = "data/raw/2022-03-13 conservative submissions"

# read file
with open(file_path, 'rb') as f:
    data = pickle.load(f)

但是,当我使用 Jupyter 笔记本时,第一个代码片段确实对我有用。因此:VSCode 中同一 src 文件夹中的 .py 和 .ipynb 文件在加载具有相对路径的文件时不使用相同的根文件夹。

我想要一个既适合我的协作者又适合我自己的解决方案,这样我们就可以运行 .py 文件而不会出现错误,也不必在每次 git Push/ 之前和之后更改路径拉。 有人可以解释一下我的误解吗?

When I want to access a pickle data file in a sibling folder, I cannot use the same (relative) paths. Because I work with multiple collaborators, this results in having to change the file_path variable (see snippets below) after each git push/pull, which is annoying, and probably unnecessary.

File structure looks like this:

workspace_folder
    -data
        -.raw
            -.-data.pickle
    -src
        -.data_analyis.py
        -.rapid_experimentation.ipynb

My collaborator wrote the following code that works on his pc in data_analysis.py:

# '..' to access sibling folder
file_path = r"../data/raw/data.pickle"

# read file
with open(file_path, 'rb') as f:
    data = pickle.load(f)

This gives me a file not found error, since VSCode assumes that the root folder is workspace_folder, and not the location of data_analysis.py. So I have to use:

# start from work_space folder, so no '..'
file_path = "data/raw/2022-03-13 conservative submissions"

# read file
with open(file_path, 'rb') as f:
    data = pickle.load(f)

However, when I use a Jupyter notebook, the first code snippet does work for me. Ergo: a .py and .ipynb file in the same src folder in VSCode do not use the same root folder when loading files with relative paths.

I would like a single solution that works for both my collaborators and myself, so that we can both run the .py file without errors or having to change the path before and after every git push/pull.
Can somebody please explain what I'm misunderstanding?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两个我 2025-01-20 19:42:20

您和您的协作者有不同的 cwd 集。看起来您的协作者已重置 cwd,例如在 settings.json 文件中添加此内容:

"python.terminal.executeInFileDir": true,

因此您协作者的 cwdsrc当您在 workspace_folder 时。

You and your collaborators have different cwd sets. It looks like your collaborators have reset the cwd, such as add this in the settings.json file:

"python.terminal.executeInFileDir": true,

So the cwd of your collaborators was src folder() while you were workspace_folder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文