gpt4 book ai didi

python-3.x - (openpyxl) FileNotFoundError : [Errno 2] No such file or directory

转载 作者:行者123 更新时间:2023-12-04 15:45:38 24 4
gpt4 key购买 nike

from os import listdir
from openpyxl import load_workbook, Workbook

files = listdir(r'C:\Users\MyID\Desktop\auto_task\examples\06')
result_xlsx = Workbook()
result_sheet =result_xlsx.active

for file in files:
if file[-4:] != 'xlsx':
continue

tg_xlsx = load_workbook(file, read_only=True)
tg_sheet = tg_xlsx.active

for row in tg_sheet.iter_rows():
row_data = []
for cell in row:
row_data.append(cell.value)
result_sheet.append(row_data)

result_xlsx.save('result.xlsx')

一直在看书练习openpyxl模块,出现错误。

FileNotFoundError: [Errno 2] No such file or directory: '2017.12.1.xlsx'

如你所见,我把具体路径写在了files = listdir....我还有一个名为 2017.12.1.xlsx

的 excel 文件

有什么问题吗?

最佳答案

您需要提供文件的完整路径 --> load_workbook(os.path.join(path, file), read_only=True)

例如:

path = r'C:\Users\MyID\Desktop\auto_task\examples\06'
files = listdir(path)
result_xlsx = Workbook()
result_sheet =result_xlsx.active

for file in files:
if file[-4:] != 'xlsx':
continue

tg_xlsx = load_workbook(os.path.join(path, file), read_only=True)
tg_sheet = tg_xlsx.active

for row in tg_sheet.iter_rows():
row_data = []
for cell in row:
row_data.append(cell.value)
result_sheet.append(row_data)

result_xlsx.save('result.xlsx')

关于python-3.x - (openpyxl) FileNotFoundError : [Errno 2] No such file or directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55918587/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com