You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VS Code的Python Jupyter单元格中打开CSV文件及解决设置工作目录与pandas读取CSV失败问题

Hey there, let's work through your two VS Code Jupyter/Pandas issues step by step—these are super common pitfalls, so you’re not alone in hitting them!

1. Opening CSV Files in VS Code Python Jupyter Cells

The easiest and most common way to work with CSV files in Jupyter cells is using the pandas library, but you can also use basic Python file operations if you just need to read raw text. Here’s how to do both:

  • Using Pandas (Recommended for Data Analysis)
    First, make sure pandas is installed (if not, run !pip install pandas in a cell). Then, you can read the CSV directly with an absolute path or a relative path (once your working directory is set correctly—we’ll fix that in the next section). Example:

    import pandas as pd
    # Absolute path example
    df = pd.read_csv("/Users/HOME/Desktop/your_file.csv")
    # Or if your working directory is already the Desktop, just use the filename
    df = pd.read_csv("your_file.csv")
    

    To quickly verify the file loaded, run df.head() to see the first few rows.

  • Basic Python File Reading
    If you just need to read the raw content without loading into a DataFrame:

    with open("/Users/HOME/Desktop/your_file.csv", "r") as f:
        content = f.readlines()
    # Print the first 5 lines to check
    print(content[:5])
    
2. Fixing Working Directory & Pandas CSV Read Failures

Let’s break this down into two parts: fixing the working directory issue, then resolving the pandas read error.

Fixing the Working Directory

The problem here is you defined the path but didn’t actually switch to it! The os.getcwd() function returns your current working directory, but to change it, you need to use os.chdir(). Here’s the corrected code:

import os
# Define your Desktop path
desktop_path = "/Users/HOME/Desktop"
# Actually switch to the Desktop directory
os.chdir(desktop_path)
# Now check the current working directory—it should show your Desktop path
print(os.getcwd())

After running this, any relative file paths (like just "your_file.csv") will look for files on your Desktop.

Resolving Pandas Read Failures

Your pd.read_csv("filename", "mode") line has a couple of issues:

  1. Incorrect parameter order: The second positional argument for pd.read_csv() is for the delimiter/separator (like ","), not the file mode. If you need to specify the mode, use it as a keyword argument: mode="r" (though this is the default, so you usually don’t need it).
  2. Missing file extension: Make sure you include .csv in the filename unless your file doesn’t have one (uncommon for CSVs).
  3. File not found: Double-check the filename spelling and that the file is actually on your Desktop (or that your working directory is set correctly).

Here’s the corrected pandas code:

import pandas as pd
# If working directory is Desktop
df = pd.read_csv("your_file.csv")
# Or use absolute path to avoid working directory issues entirely
df = pd.read_csv("/Users/HOME/Desktop/your_file.csv")

If you still get an error, check the exact error message—it’ll tell you if the file doesn’t exist, there’s a permission issue, or a formatting problem with the CSV itself. For example, if the error is FileNotFoundError, triple-check the path and filename.

内容的提问来源于stack exchange,提问作者Teddy Chief Chikwane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 05:12:42