Python Forum
How to list out specific excel files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to list out specific excel files
#1
Hi


iam beginner to python. i want to list out xlsx & xlsm (i no need "xls" files) files from a directory (or including all subdirectories) using python


Regards
Ajay
Reply
#2
A starting point:
import os

for file in os.scandir():
    if file.is_dir():
        print('/', file.name, sep='')
    else:
        print(file.name)
When I run this I see:
Output:
model.py part_test.py regex.py sandbox.py __init__.py /__pycache__
Another interesting function in the os module are chdir(). You can read about them here: https://docs.python.org/2/library/os.html
Reply
#3
or with pathlib:
>>> from pathlib import Path
>>> p = Path('.')
>>> excelfiles = [x for x in p.iterdir() if x.is_file() and (x.suffix == '.xlsx' or x.suffix == '.xlsm')]
>>> for filename in excelfiles:
...     print(filename.name)
... 
PreliminaryDataByCongressionalDistrict2002.xlsx
PreliminaryDataByCongressionalDistrict2004.xlsx
PreliminaryDataByCongressionalDistrict2006.xlsx
PreliminaryDataByCongressionalDistrict2008.xlsx
PreliminaryDataByCongressionalDistrict2010.xlsx
PreliminaryDataByCongressionalDistrict2012.xlsx
PreliminaryDataByCongressionalDistrict2014.xlsx
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to export files from spyder to excel? kranklin 3 1,966 Aug-04-2025, 08:51 PM
Last Post: kranklin
  Copy Paste excel files based on the first letters of the file name Viento 2 2,383 Feb-07-2024, 12:24 PM
Last Post: Viento
  Search Excel File with a list of values huzzug 4 5,058 Nov-03-2023, 05:35 PM
Last Post: huzzug
  trouble reading string/module from excel as a list popular_dog 0 1,563 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  list the files using query in python arjunaram 0 1,474 Mar-28-2023, 02:39 PM
Last Post: arjunaram
  python print all files which contain specific word in it mg24 5 3,343 Jan-27-2023, 11:20 AM
Last Post: snippsat
  python move specific files from source to destination including duplicates mg24 3 2,643 Jan-21-2023, 04:21 AM
Last Post: deanhystad
  How to loop through all excel files and sheets in folder jadelola 1 12,790 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  Creating csv files from Excel file azizrasul 40 18,118 Nov-03-2022, 08:33 PM
Last Post: azizrasul
  search a list or tuple for a specific type ot class Skaperen 8 4,752 Jul-22-2022, 10:29 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020