Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to access all xlsx files in all subdirectories?
#1
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()
Quote
#2
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
Quote
#3
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
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Random access binary files with mmap - drastically slows with big files danart 1 472 Jun-17-2019, 10:45 AM
Last Post: danart
  reading .xls files and saving as .xlsx jon0852 1 3,555 Oct-17-2017, 08:32 PM
Last Post: buran
  Looping .xlsx files in folder/subfolders copy pasting currentregion HarrisQ 4 2,471 Apr-17-2017, 06:35 AM
Last Post: HarrisQ

Forum Jump:


Users browsing this thread: 1 Guest(s)