找不到"/>
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:解决命名文件在,但是报错找不到
发布评论