VSCode:FileNotFoundError:[Errno 2] 没有这个文件或目录

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

VSCode : FileNotFoundError: [Errno 2] No such file or directory

问题

我刚开始学习Python正在使用VSCode想要导入一个csv文件但是我做不到

这是我的代码

```python
import pandas as pd
df = pd.read_csv(r'C:\\Users\\Documents\\test.csv')
print(df)

但是它给了我一个错误:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Documents\\test.csv'

我尝试了很多方法,路径和文件名都没有错误。有人有办法可以帮帮我吗?


<details>
<summary>英文:</summary>

I&#39;m new with python, I&#39;m working with VSCode and I want to import a csv file but I can&#39;t.



here is my code :

import pandas as pd
df = pd.read_csv(r'C:\Users\Documents\test.csv')
print(df)

and it gives me :

FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\Documents\test.csv'


I tried a lot of things and there is no mistakes in the path or file name. 
Did someone have an idea to help me ?

</details>


# 答案1
**得分**: 2

```python
要么转义反斜杠,要么使用原始字符串,不能同时使用

```python
import pandas as pd
df = pd.read_csv(r'C:\Users\Documents\test.csv')
print(df)

更新后的编辑

%USERPROFILE% 将返回当前用户的配置文件路径,例如在我的情况下是 C:\Users\Lenovo,所以你的路径可能是 C:\Users\Lenovo\Documents\test.csv

import pandas as pd
df = pd.read_csv(r'%USERPROFILE%\Documents\test.csv')
print(df)
英文:

Either escape the backslashes or use raw strings, not both

import pandas as pd
df = pd.read_csv(r&#39;C:\Users\Documents\test.csv&#39;)
print(df)

Updated Edit

%USERPROFILE% will return your current user's profile path, such as C:\Users\Lenovo in my case. so your path could be C:\Users\Lenovo\Documents\test.csv

import pandas as pd
df = pd.read_csv(r&#39;%USERPROFILE%\Documents\test.csv&#39;)
print(df)

答案2

得分: 0

也许这会解决它

import pandas as pd
df = pd.read_csv(r'C:\Users\Documents\test.csv')
print(df)
英文:

maybe this will fix it

import pandas as pd
df = pd.read_csv(r&#39;C:\Users\Documents\test.csv&#39;)
print(df)

huangapple
  • 本文由 发表于 2023年6月22日 19:34:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531453.html
匿名

发表评论

匿名网友

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

确定