【问题标题】:What does the Permission error mean when trying to read in Excel files using the xlrd module?尝试使用 xlrd 模块读取 Excel 文件时出现权限错误是什么意思?
【发布时间】:2019-02-21 20:46:12
【问题描述】:

我是 python 新手,我只是想弄清楚如何使用 xlrd 模块从 Excel 中读取数据集。当我运行我的代码时,我收到了权限错误 [errno 13]。我不确定错误是什么意思或为什么我得到它。 这是我正在使用的代码:

import xlrd
loc = ("path to the file I'm trying to read in")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0,0)
print(sheet.nrows)

这是我得到的输出:

Traceback(最近一次调用最后一次):文件“GaitOptMain.py”,第 46 行, 在 wb = xlrd.open_workbook(loc) 文件 "C:\Users\mleef\AppData\Local\Programs\Python\Python37\lib\site-packages\xlrd__init__.py", 第 116 行,在 open_workbook 中 with open(filename, "rb") as f: PermissionError: [Errno 13] Permission denied: [path that I used in the code]

【问题讨论】:

  • 您有权访问该文件吗?有密码保护吗?您可以在没有任何问题/提示的情况下使用 Excel 打开吗?
  • 是的,我可以在没有密码或提示的情况下访问这些文件。
  • 是否可以在 excel 中打开文档?如果我没记错,excel 会阻止您打开的工作簿。

标签: python excel python-3.x


【解决方案1】:

或者如果你已经打开了文件,你会看到这个错误

【讨论】:

  • 在读取之前如何确保文件在之前的执行中已经关闭?
【解决方案2】:

我实际上能够弄清楚。我认为问题在于我试图读取目录而不是实际文件。我使用的路径结束于文件夹而不是文件。

    loc = ("C:/Users/mleef/Desktop/python text/practice_data.xlsx")
    wb = xlrd.open_workbook(loc)
    sheet = wb.sheet_by_index(0)
    sheet.cell_value(0,0)
    print(sheet.nrows)

输出: 1429(如果数据集中的行数)

【讨论】:

    猜你喜欢
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 2021-05-03
    相关资源
    最近更新 更多