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
  Copy Paste excel files based on the first letters of the file name Viento 2 406 Feb-07-2024, 12:24 PM
Last Post: Viento
  Search Excel File with a list of values huzzug 4 1,211 Nov-03-2023, 05:35 PM
Last Post: huzzug
  trouble reading string/module from excel as a list popular_dog 0 413 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  list the files using query in python arjunaram 0 666 Mar-28-2023, 02:39 PM
Last Post: arjunaram
  python print all files which contain specific word in it mg24 5 1,226 Jan-27-2023, 11:20 AM
Last Post: snippsat
  python move specific files from source to destination including duplicates mg24 3 1,090 Jan-21-2023, 04:21 AM
Last Post: deanhystad
  How to loop through all excel files and sheets in folder jadelola 1 4,441 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  Creating csv files from Excel file azizrasul 40 5,516 Nov-03-2022, 08:33 PM
Last Post: azizrasul
  search a list or tuple for a specific type ot class Skaperen 8 1,913 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  list from excel devilonline 4 1,349 Jun-22-2022, 11:00 PM
Last Post: devilonline

Forum Jump:

User Panel Messages

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