FileNotFoundError: [Errno 2] No such file or directory:解决命名文件在,但是报错找不到

编程入门 行业动态 更新时间:2024-10-11 09:25:37

FileNotFoundError: [Errno 2] No such file or directory:解决命名文件在,但是报错<a href=https://www.elefans.com/category/jswz/34/1771416.html style=找不到"/>

FileNotFoundError: [Errno 2] No such file or directory:解决命名文件在,但是报错找不到

‘’’
from openpyxl import load_workbook
import csv, os

#os.makedirs(‘E:\pdf\python资料\python 快速上手实践automate_online-materials\excelspread’,exist_ok=True)
for excelFile in os.listdir(‘E:\pdf\python资料\python 快速上手实践automate_online-materials\excelspread’):

  if not excelFile.endswith('.xlsx'):continue#wb = load_workbook(excelFile,data_only=True)wb = load_workbook(excelFile)for sheetName in wb.sheetnames:ws = wb[sheetName]File = open(excelFile.replace('.xlsx','')+'_'+sheetName+ '.csv','w',newline='')rowData = []for row in range(1,ws.max_row+1):for col in range(1,ws.max_column+1):rowData.append(ws.cell(row = row,column= col).value)csvWriter = csv.writer(File)csvWriter.writerow(rowData)rowData = []File.close()

print(‘Done’)
‘’’

===============================================
在os.listdir(’ ')中找xlsx文件时,找到后,文件名并不带有路径前缀,如果不是在根目录下运作,仅有文件名的文件无法正常读取,会报错。
为解决此类问题,可以在wb.load_workbook(‘E:\pdf\python资料\python 快速上手实践automate_online-materials\excelspread’ + ecelFIle),这样才不会报错

更多推荐

FileNotFoundError: [Errno 2] No such file or directory:解决命名文件在,但是报错找不到

本文发布于:2024-03-08 13:15:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1720981.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:找不到   报错   文件   Errno   FileNotFoundError

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!