简体   繁体   English

VS 代码不将相对路径识别为文件名

[英]VS code do not recognise relative path as filename

I have the following folder structure我有以下文件夹结构

---data/
    |-----test.csv
---pythonscript/
    |-----myscript.ipynb
    |-----myscript.py

The following codes work just fine in myscript.ipynb :以下代码在myscript.ipynb中运行良好:

import pandas as pd
df1=pd.read_csv('../data/test.csv')

However, when I moved the same codes to myscript.py and run it using VS code, it shows me the FileNotFoundError that '../data/test.csv' does not exist.但是,当我将相同的代码移动到myscript.py并使用 VS 代码运行它时,它会显示FileNotFoundError '../data/test.csv'不存在。 I checked:我检查了:

  • both jupyter notebook and VScode are using Python 3.8.3 64-bit ('base':conda) interpreter jupyter notebook 和 VScode 都在使用 Python 3.8.3 64-bit ('base':conda) 解释器
  • They have the same cwd as pythonscript/它们具有与pythonscript/相同的 cwd
  • When I use absolute path of test.csv on VScode instead, it works also fine.当我在test.csv上使用 test.csv 的绝对路径时,它也可以正常工作。

Because I are sharing the codes with a peer, I want to avoid typing absolute path as possible.因为我与同行共享代码,所以我想尽可能避免输入绝对路径。

The temporary solution I came up with, is to have additionally:我想出的临时解决方案是另外:

import os
os.chdir("..")
f1=os.getcwd()+'/data/'
df1=pd.read_csv(f1+'test.csv')

But it is not really succinct.但这并不简洁。 I wonder if there is a standard solution for this kind of annoyance in VS code?我想知道是否有针对 VS 代码中这种烦恼的标准解决方案? I am very new to VS code.我对 VS 代码很陌生。 Thanks!谢谢!

According to the information you described, I created the relevant files, and the following are the results of my testing in different folder structures:根据您描述的信息,我创建了相关文件,以下是我在不同文件夹结构中测试的结果:

在此处输入图像描述

在此处输入图像描述

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

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