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?
#5
I agree with Dead_EyE, Path is very useful, because it will work for different operating systems and it has other tricks up its sleeve!

if filename.is_file() means you won't pick up directories or their contents.

from pathlib import Path
import sys 
 
# mydir looks like: PosixPath('/home/pedro/temp')
mydir = Path('/home/pedro/temp')
# create a generator: filelist <generator object <genexpr> at 0x7ad729970900>
filelist = (filename for filename in mydir.iterdir() if filename.is_file())
# create a list of files
file_list = [filename for filename in mydir.iterdir() if filename.is_file()]

# have a look at the files
for f in filelist:
    print(f)

# use Path to look at the files
# need to recreate the generator
filelist = (filename for filename in mydir.iterdir() if filename.is_file())

for filename in filelist:
    print(f"\nfilename: {filename.name}")
    print(f"file suffix: {filename.suffix}")
    print(f"full path: {filename.resolve()}")
    print(f"filepath parts: {filename.parts}")
The advantage of the generator is size. Imagine you had millions of files. The list would be very big in memory. The generator is tiny!

sys.getsizeof(filelist) # returns 104
sys.getsizeof(file_list) # returns 472 nearly 5 times bigger than filelist
Reply


Messages In This Thread
RE: Loop through all files in a directory? - by Pedroski55 - Apr-23-2024, 05:40 AM

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