C:\Users\we\Desktop\python文件\pythonProject2\.venv\Scripts\python.exe C:\Users\we\Desktop\尚梦秋-241212201205\实验报告\基于前馈神经网络完成鸢尾花分类任务\1.py Traceback (most recent call last): File "C:\Users\we\Desktop\尚梦秋-241212201205\实验报告\基于前馈神经网络完成鸢尾花分类任务\1.py", line 49, in <module> df = pd.read_csv(r'C:\Users\we\Desktop\尚梦秋-241212201205\实验报告\基于前馈神经网络完成鸢尾花分类任务', usecols=[1, 2, 3, 4, 5]) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\app\anaconda\Lib\site-packages\pandas\io\parsers\readers.py", line 1026, in read_csv return _read(filepath_or_buffer, kwds) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\app\anaconda\Lib\site-packages\pandas\io\parsers\readers.py", line 620, in _read parser = TextFileReader(filepath_or_buffer, **kwds) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\app\anaconda\Lib\site-packages\pandas\io\parsers\readers.py", line 1620, in __init__ self._engine = self._make_engine(f, self.engine) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\app\anaconda\Lib\site-packages\pandas\io\parsers\readers.py", line 1880, in _make_engine self.handles = get_handle( ^^^^^^^^^^^ File "D:\app\anaconda\Lib\site-packages\pandas\io\common.py", line 873, in get_handle handle = open( ^^^^^ PermissionError: [Errno 13] Permission denied: 'C:\\Users\\we\\Desktop\\尚梦秋-241212201205\\实验报告\\基于前馈神经网络完成鸢尾花分类任务' 进程已结束,退出代码为 1 、
时间: 2025-06-01 19:53:50 浏览: 20
### 解决方案
当使用 `pd.read_csv` 函数读取 CSV 文件时,如果遇到 `FileNotFoundError` 或 `PermissionError` 错误,通常是因为以下几个原因:
#### 1. **文件路径错误**
如果指定的文件路径不存在,则会抛出 `FileNotFoundError`。为了防止这种情况发生,可以先验证文件是否存在再执行读取操作。
使用 Python 的标准库 `os.path.exists()` 方法来检查文件路径的有效性[^1]。
```python
import os
file_path = 'property-data.csv'
if not os.path.exists(file_path):
raise FileNotFoundError(f"The file {file_path} does not exist.")
```
#### 2. **相对路径 vs 绝对路径**
当提供的是相对路径而不是绝对路径时,可能会因为当前工作目录的不同而导致找不到文件。建议始终使用绝对路径以减少不确定性。
可以通过以下方式获取当前脚本所在的工作目录并拼接文件名形成完整的路径:
```python
import os
current_dir = os.getcwd()
absolute_file_path = os.path.join(current_dir, 'property-data.csv')
df = pd.read_csv(absolute_file_path, encoding='utf-8') # 假设编码为 utf-8
```
#### 3. **权限不足 (PermissionError)**
如果程序尝试访问受保护的文件或者运行环境缺乏必要的权限,则会出现 `PermissionError`。可以通过更改文件权限或调整代码逻辑来解决问题。
- 对于本地开发环境中的文件,确保该文件具有足够的读取权限。
在 Linux/MacOS 上可使用命令修改权限:
```bash
chmod +r property-data.csv
```
- 若是在服务器环境中部署应用,需确认服务账户拥有对该文件夹及其子资源的访问权。
#### 4. **网络驱动器上的文件**
如果目标 CSV 存储在网络位置而非本地磁盘上,可能需要额外配置才能成功加载数据。例如,在 Windows 平台上映射远程共享作为虚拟盘符;而在 Unix-like 系统里则挂载 NFS/SMB 卷到特定节点下。
此外,某些情况下也可以借助 URL 来代替传统 FS 路径完成下载与解析过程:
```python
url = "http://example.com/remote/path/to/data.csv"
df = pd.read_csv(url)
```
综上所述,针对上述两种异常情况采取适当措施即可有效规避风险[^5]。
```python
import pandas as pd
import os
def load_data(file_name, encoding='utf-8'):
"""
Safely loads a CSV into a DataFrame while handling common errors.
Parameters:
file_name (str): The name or full path of the target CSV file.
encoding (str): Encoding type used by the CSV.
Returns:
pd.DataFrame: A dataframe containing loaded data from csv.
Raises:
Exception: If any error occurs during loading process.
"""
abs_path = os.path.abspath(file_name)
try:
if not os.path.isfile(abs_path):
raise FileNotFoundError(f"No such file found at '{abs_path}'.")
return pd.read_csv(abs_path, encoding=encoding)
except PermissionError:
print(f"Insufficient permissions to read file located at '{abs_path}'.")
raise
except UnicodeDecodeError:
print("Detected decoding issue; please verify correct character set is specified.")
raise
if __name__ == "__main__":
dataset = load_data('property-data.csv', encoding='gbk')
print(dataset.head())
```
阅读全文
相关推荐


















