Python Forum
Loop through all files in a directory?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop through all files in a directory?
#1
Hello,

I need to find all the files located under a given directory, including all the sub-directories it may contain.

Is this a/the right way to do it?

import os
import glob

for filename in glob.iglob(r".\parent" '**/**', recursive=True):
  #ignore sub-directories, just grab files
  if not os.path.isdir(filename):
    print(filename)
Thank you.
Reply


Messages In This Thread
Loop through all files in a directory? - by Winfried - Apr-22-2024, 06:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Filer and sort files by modification time in a directory tester_V 5 389 May-02-2024, 05:39 PM
Last Post: tester_V
  [SOLVED] Loop through directories and files one level down? Winfried 3 327 Apr-28-2024, 02:31 PM
Last Post: Gribouillis
  File loop curiously skipping files - FIXED mbk34 10 965 Feb-10-2024, 07:08 AM
Last Post: buran
  uploading files from a ubuntu local directory to Minio storage container dchilambo 0 524 Dec-22-2023, 07:17 AM
Last Post: dchilambo
  change directory of save of python files akbarza 3 998 Jul-23-2023, 08:30 AM
Last Post: Gribouillis
  Using pyinstaller with .ui GUI files - No such file or directory error diver999 3 3,638 Jun-27-2023, 01:17 PM
Last Post: diver999
  Monitoring a Directory for new mkv and mp4 Files lastyle 3 1,716 May-07-2023, 12:33 PM
Last Post: deanhystad
  How to loop through all excel files and sheets in folder jadelola 1 4,622 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  Read directory listing of files and parse out the highest number? cubangt 5 2,476 Sep-28-2022, 10:15 PM
Last Post: Larz60+
  How to save files in a separate directory Scordomaniac 3 2,132 Mar-16-2022, 10:17 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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