Python Forum

Full Version: How to access all xlsx files in all subdirectories?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
How to modify this program if I have more dirs in dir1 to get all xlsx.?
So the xls files are within different dirs in dir1, like
dir1\1990\1.xlsx
dir1\1991\123.xlsx
dir1\1992\124.xlsx

    import glob
    from win32com.client import Dispatch
    
    
    for file in glob.glob('C:\Users\igyulavics\Desktop\dir1\*.xlsx'):
        xl = Dispatch('Excel.Application')
        wb = xl.Workbooks.Add(file)
        wb.SaveAs(file[:-1], FileFormat=56)
        xl.Quit()
glob.glob(r'C:\Users\igyulavics\Desktop\*.xlsx', recursive=True)
use recursive=True in glob

Note that you need to pass raw string if using backslash or escape the backslash or use forward slash in path
With pathlib you get a better abstraction.

from pathlib import Path

document_dir = Path(r'C:\Users\igyulavics\Desktop\dir1')
for xlsx_file in document_dir.glob('**/*.xlsx'):
    # xlsx_file is a Path object
    # if you use old libraries, you have to use str(xlsx_file) to convert the Path to a str
    print(xlsx_file)
This code finds all .xlsx files recursive. This should work also on Windows.
Look here: https://docs.python.org/3/library/pathli....Path.glob