Hello,
I recently started writing a small program for reading multiple xls files from one directory.
While reading one file like this,works perfectly:

wb = xlrd.open_workbook("C:\\path\\FileName.xls")
    sh = wb.sheet_by_index(0)

this one doesn't:

os.chdir("C:\\path")
for fileName in os.listdir("."):
    wb = xlrd.open_workbook(fileName)
    sh = wb.sheet_by_index(0)

I keep getting the same error:
XLRDError: Expected BOF record; found 0x614d
I have no idea what I am doing wrong, please help.

Dani AI

Generated

Short note for context: ran into a BOF/beginning‑of‑file parsing error when looping a directory; ’s suggestion to filter by extension is a good first step, but the root cause is often that the file contents don’t actually match the old BIFF .xls format (examples: the file is HTML/XML/CSV, it’s actually a zipped .xlsx, or the file is truncated/corrupt). (stackoverflow.com)

Practical checklist to make the loop robust:

  • Only process real files (skip directories and hidden/temp files Excel creates, e.g. names beginning with the tilde+dollar prefix). (learn.microsoft.com)
  • Quick‑inspect the first bytes to confirm format before handing the file to a parser: an old-style .xls is an OLE/CFB file, newer .xlsx files are ZIPs that start with “PK…”, XML/HTML begin with printable text. Use that to skip mismatches early. (garykessler.net)

Example (safe) workflow to run before actual parsing:

import glob, os

def looks_like_old_xls(path):
    with open(path, 'rb') as f:
        hdr = f.read(8)
    return hdr.startswith(b'\xD0\xCF')  # fast check for OLE/BIFF (.xls)

for path in glob.glob(r'C:\path\*.xls'):
    name = os.path.basename(path)
    if name.startswith('~$') or not os.path.isfile(path):
        continue
    if not looks_like_old_xls(path):
        print('skip (format mismatch):', path)
        continue
    process_xls(path)   # call your parsing routine here

If files are actually .xlsx (or mixed), use a modern reader: recent xlrd versions only support old .xls; prefer openpyxl or pandas with engine='openpyxl' for .xlsx files. Also wrap each open in try/except, log the filename and exception, and try opening the suspicious file in a text editor (or Excel) and resave in the expected format if needed. (pandas.pydata.org)

Summary: filter and skip temp files, verify file signature before parsing, and choose the correct engine for the file format. This removes most “BOF/expected header” surprises and makes directory processing reliable.

Recommended Answers

All 2 Replies

It could be that there are files other than XLS in the folder. Try 'if files.endswith(".xls")'

No, there were only .xls files in folder. Anyway because all the files had the same name and only numbers at the end of it were different i just did this, silly solution but it worked:

for y in numbers:
wb = xlrd.open_workbook("C:\\path\\FileName" + str(y)+ ".xls")

numbers is a list containing all numbers that appear in file names.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.