Python Forum
How to access all xlsx files in all subdirectories? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to access all xlsx files in all subdirectories? (/thread-16889.html)



How to access all xlsx files in all subdirectories? - Krszt - Mar-19-2019

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()



RE: How to access all xlsx files in all subdirectories? - buran - Mar-19-2019

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


RE: How to access all xlsx files in all subdirectories? - DeaD_EyE - Mar-19-2019

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/pathlib.html#pathlib.Path.glob