Python Forum
[SOLVED] Loop through directories and files one level down?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Loop through directories and files one level down?
#4
(Apr-28-2024, 02:09 PM)Winfried Wrote: This finally works:
You don't need to change the process' working directory for every subdirectory. A better code is
from pathlib import Path

for subdir in Path(ROOT).iterdir():
    if subdir.is_dir():
        for file in subdir.glob('*.html'):
            print(file)
« We can solve any problem by introducing an extra level of indirection »
Reply


Messages In This Thread
RE: Loop through directories and files one level down? - by Gribouillis - Apr-28-2024, 02:31 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop through all files in a directory? Winfried 10 787 Apr-23-2024, 07:38 PM
Last Post: FortuneCoins
  Organization of project directories wotoko 3 582 Mar-02-2024, 03:34 PM
Last Post: Larz60+
  File loop curiously skipping files - FIXED mbk34 10 1,066 Feb-10-2024, 07:08 AM
Last Post: buran
Question [solved] compressing files with python. SpongeB0B 1 723 May-26-2023, 03:33 PM
Last Post: SpongeB0B
  [SOLVED] [loop] Exclude ranges in… range? Winfried 2 1,672 May-14-2023, 04:29 PM
Last Post: Winfried
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,372 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Help replacing word in Mutiple files. (SOLVED) mm309d 0 925 Mar-21-2023, 03:43 AM
Last Post: mm309d
  Listing directories (as a text file) kiwi99 1 912 Feb-17-2023, 12:58 PM
Last Post: Larz60+
  Find duplicate files in multiple directories Pavel_47 9 3,448 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  How to loop through all excel files and sheets in folder jadelola 1 4,783 Dec-01-2022, 06:12 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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