使用pandas read_csv读取文件遇ParserError与PermissionError,求排查
Let's break down the two issues you're facing one by one:
1. Initial ParserError: Tokenizing Data Failure
The first error pops up because pandas' default c engine is fast but extremely strict about CSV formatting. Common triggers include:
- Inconsistent column counts across rows (e.g., some lines have extra or missing commas)
- Unclosed quotation marks in the file
- The file isn't actually a valid CSV (maybe it's a different format, or got corrupted)
Switching to engine='python' is a smart first move here—this engine is more flexible and can handle minor formatting glitches that break the stricter C engine. But then you hit a permission error, which is a separate system-level problem unrelated to pandas itself.
2. PermissionError: Errno 13
This error means your Python process doesn't have the necessary rights to access the file. Here are the most common fixes to try:
- Close the file if it's open elsewhere: If the CSV is open in Excel, Notepad, or another program, it might be locked. Close those apps and retry.
- Verify the file path: Make sure you're pointing to the actual file, not a folder. Using an absolute path (like
C:/Users/YourName/Documents/data.csvon Windows or/home/yourname/data.csvon Linux/macOS) can eliminate path confusion. - Adjust file permissions:
- On Windows: Right-click the file → Properties → Security tab → Confirm your user account has "Read" permissions enabled.
- On Linux/macOS: Open a terminal, navigate to the file's directory, and run
chmod +r filename.csvto grant read access.
- Move the file to a non-protected folder: If you're trying to read from a restricted directory (like Program Files on Windows or root directories on Linux), move the file to a user-owned folder (e.g., Documents, Desktop) and try again.
Quick Extra Tip
Before messing with engine settings, open the CSV in a plain text editor (like Notepad++ or VS Code) to spot obvious formatting issues—this can save you time troubleshooting parser errors later.
内容的提问来源于stack exchange,提问作者BorisA

