Python中CSV读取器持续报FileNotFoundError求助
Hey there! Let's work through this file path issue together—it's super common when you're starting out with file handling in Python, so you're not alone here.
First, Let's Spot the Mismatch
Looking at your error message:
FileNotFoundError: [Errno 2] No such file or directory: '..\Downloads\netflix_ratings.csv.csv'
But your code uses this path:
csvpath = os.path.join("C:", "Desktop", "Python2", "netflix_ratings.csv.csv")
The problem is clear: your code is looking for the file on your Desktop, but the error shows the file is actually in your Downloads folder. That's why Python can't find it!
Step-by-Step Fixes
Get the correct file path
Locate yournetflix_ratings.csv.csvfile in File Explorer. Right-click it, select "Properties", then copy the "Location" (e.g.,C:\Users\YourName\Downloads). Use that location to build your path in code:csvpath = os.path.join("C:", "Users", "YourName", "Downloads", "netflix_ratings.csv.csv")Replace
YourNamewith your actual Windows username.Verify the path before opening the file
Add a print statement to check if your path is correct—this helps catch typos early:print("Checking file path:", csvpath)Copy the printed path into File Explorer's address bar; if it opens your file, you know the path is right.
Fix the double .csv suffix (optional but clean)
That.csv.csvsuffix probably happened because Windows hides file extensions by default. To fix this:- Open File Explorer, go to "View" > check "File name extensions".
- Rename the file to
netflix_ratings.csvand update your code to match the new filename.
Modified Working Code
Here's your code with the fixes applied (don't forget to replace the path with your actual one):
import os import csv video = input("What show or movie are you looking for? ") # Replace with your actual file path csvpath = os.path.join("C:", "Users", "YourName", "Downloads", "netflix_ratings.csv.csv") print("Checking file path:", csvpath) found = False try: with open(csvpath, newline="") as csvfile: csvreader = csv.reader(csvfile, delimiter=",") # Uncomment the line below if your CSV has a header row # next(csvreader) for row in csvreader: if row[0] == video: print(f"{row[0]} is rated {row[1]} with a rating of {row[5]}") found = True except FileNotFoundError: print(f"Oops! Couldn't find the file at {csvpath}. Double-check the path and filename.") if not found: print("Sorry about this, we don't seem to have what you are looking for!")
内容的提问来源于stack exchange,提问作者Dessamol

