正在使用VSCode时遇到文件目录错误。

huangapple go评论95阅读模式
英文:

Am facing an error in vscode with file directories

问题

每当我尝试在Jupyter笔记本中调用文件进行分析,例如导入CSV文件时,我观察到由于某些原因,路径中会额外添加一个"\"。
错误截图
我不知道如何解决它。
请帮助。

我尝试使用过时的路径,但仍然面临相同的问题。

英文:

When ever I try call a file inside the jupyter notebook to analysis for example importing a csv file.
I observed due to some reason a extra " " is being added to the path.
Screen shot of error
I don't know how to resolve it.
Please help.

I tried to use obsolete path but still facing same issue

答案1

得分: 1

问题出在你提供的反斜杠的类型上。尝试将 \ 替换为 /,应该可以工作。或者简单地右键单击在vscode目录中呈现的文件,并在下方复制路径或相对路径,然后将其简单粘贴到这里。它将会工作。

英文:

The Problem lies in the type of backslash you have given. Try changing \ with / and it should work. or simply right click on the file presented at vscode directory and at down copy the path or relative path and simply paste it here. It will work.

答案2

得分: 0

Replace the \ with /

\是特殊字符的前缀。

英文:

Replace the \ with /

\ is a prefix for a special character.

答案3

得分: 0

这个问题是由于在路径中使用反斜杠导致的,反斜杠在Python中被视为转义字符。

解决方法:

使用原始字符串 rdf = pd.read_csv(r'Data\sample_submission.csv')

英文:

This issue happens due to the use of backslash in the path, The backslash is treated as a escape character in python.

To resolve:

Use raw string 'r', df = pd.read_csv(r'Data\sample_submission.csv')

答案4

得分: 0

如果您想添加***以指示文件路径,您可以尝试*/(斜杠)\(双斜杠)**。

例如

'Data\\sample_submission.csv';

'Data/sample_submission.csv';

确保您的文件首先放在正确的路径中。

英文:

If you want to add ** to indicate file path, you can try /(slash) or \ \ (double backslash).

for example

'Data\\sample_submission.csv'

or

'Data/sample_submission.csv'

make sure your file is placed in right path first.

答案5

得分: -1

有两种在Python中实现路径的方法。

  1. 绝对路径(我不建议使用)
  2. 相对路径

要创建一个相对路径,您必须以./开头,例如./Test.txt将指向项目文件夹内的Test.txt文件(主文件所在的位置)。

如果您想访问上一级根目录中的文件夹但仍然与源文件相关,可以使用..来导航到上一级文件夹,例如./../将指向项目文件夹所在的文件夹。

请注意,即使在不是主要Python文件的模块中使用它,相对路径也将从您的主文件计算。

在您的情况下

df = pd.read_csv('./Data/sample_submission.csv')

额外提示:使用/\\来定义路径,使用\将会破坏您的路径,因为它是转义字符。
> https://www.w3schools.com/python/gloss_python_escape_characters.asp

英文:

There are 2 ways to implement a path in Python.

  1. Absolute (Which I don't suggest)
  2. Relative

To create a relative path you must start it with ./, for example ./Test.txt will point to the Test.txt file inside the project folder (Where the main file exists)

If you want access a folder in upper roots but still relative to the source you can use .. to navigate a folder back, for example ./../ will point to the folder that the project folder is placed in.

Keep in mind, Even if you are using it in a module that is not your main Python file, Relative paths will be calculated from your main file.

In your case

df = pd.read_csv('./Data/sample_submission.csv')

Extra tip: Use / or \\ to define paths, Using \ will break your path as it's the escape character
> https://www.w3schools.com/python/gloss_python_escape_characters.asp

huangapple
  • 本文由 发表于 2023年7月23日 15:23:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定