首页 > 解决方案 > 为什么我在尝试打开 Excel 文件时收到“PermissionError: [Errno 13] Permission denied”?

问题描述

您好我正在尝试在 python 中访问一个 excel 文件。我正在使用我们在课堂上学到的这段代码:

with open("file_name") as csv_file:
    csv_reader =csv.DictReader(csv_file)

每次我运行它我都会得到同样的错误

PermissionError: [Errno 13] Permission denied: 'file_name' 

是否有任何我可以运行的代码允许命令 shell 读取文件?

标签: python

解决方案


您需要file_name使用正确的路径和文件名进行更改,即:

import csv
with open("/home/user/path/to/file.csv") as csv_file:
    csv_reader = csv.DictReader(csv_file)

对于 windows 将如下所示:

import csv
with open("c:\\path\\to\\file.csv") as csv_file:
    csv_reader = csv.DictReader(csv_file)

推荐阅读