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?
#10
TypeHints are not required, but they help the developers of libraries to communicate what a method/function is expecting and what it should return.

Quote:What I don't like with the pathlib solution is the call to .is_file() for every path, which does a system call while os.walk() produces the list of files with internal mechanism. This needs to be checked but I suspect that os.walk() does less system calls.

It does lesser calls and is faster.

Output:
public@ganymed:~$ for script in walk?.py; do echo -n "$script: "; strace -e newfstatat python3 $script 2>&1 | wc -l; done walk1.py: 27556 walk2.py: 129893 walk3.py: 11484
Output:
public@ganymed:~$ for script in walk?.py; do echo -n "$script: "; time python3 $script; echo ; done walk1.py: real 0m0,351s user 0m0,192s sys 0m0,160s walk2.py: real 0m1,208s user 0m0,883s sys 0m0,325s walk3.py: real 0m0,285s user 0m0,179s sys 0m0,106s
pathlib.Path.walk were added since Python 3.12: https://docs.python.org/3/library/pathli....Path.walk
This is similar to os.walk, but has some differences with the handling of symlinks.

The last example uses
Output:
Path.walk()
walk1.py
import os


for root, dirs, files in os.walk("/usr"):
    for file in files:
         ...
walk2.py
from pathlib import Path


for element in Path("/usr").rglob("*"):
    element.is_file()
walk3.py
from pathlib import Path


for root, dirs, files in Path("/usr").walk():
    for file in files:
        pass
Gribouillis likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Loop through all files in a directory? - by DeaD_EyE - Apr-23-2024, 09:14 AM

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