英文:
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'm new with python, I'm working with VSCode and I want to import a csv file but I can'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'C:\Users\Documents\test.csv')
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'%USERPROFILE%\Documents\test.csv')
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'C:\Users\Documents\test.csv')
print(df)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论