为什么在尝试打开excel文件时会出现“PermissionError:[Errno 13]Permission denied”?

2024-05-23 22:48:31 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,我正在尝试访问python中的excel文件。我用的是我们在课堂上学到的代码:

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

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

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

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


Tags: 文件csv代码nameas错误withopen
1条回答
网友
1楼 · 发布于 2024-05-23 22:48:31

您需要用正确的路径和文件名更改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)

相关问题 更多 >