troubleshooting Question

Using Python how do you bypass or move an excel file in a folder if it is corrupt?

Avatar of crompnk
crompnkFlag for United Kingdom of Great Britain and Northern Ireland asked on
Python* python3
1 Comment1 Solution225 ViewsLast Modified:
Hi,

I am using the following Python code in a bigger script, but when some excel files (.xls) are corrupt I get the following error:

raise XLRDError('Unsupported format, or corrupt file: ' + msg)

XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'X\xcf\x13\xe0\xa1\xb9\x1a\xe1'


It fails when opening a workbook that is corrupt (wb = xlrd.open_workbook(filepath)), which ends the script. Is there a way I can modify the script to bypass the corrupt excel file or move it somewhere else and allow the script to move on?


import pandas as pd
import os
import xlrd
import shutil
from os import path

for subdir, dirs, files in os.walk(r'C:\Temp'):
    for file in files:
        filepath = subdir + os.sep + file
        
        
        wb = xlrd.open_workbook(filepath)
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros