0% found this document useful (0 votes)
24 views17 pages

Getting A List of All Files and Folders

This document provides a guide on how to list all files and folders in a directory using Python's pathlib module. It explains the use of methods like .iterdir(), .rglob(), and .glob() for retrieving directory contents, including recursive listing and conditional filtering with glob patterns. Examples demonstrate how to implement these methods to access and display files and folders effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views17 pages

Getting A List of All Files and Folders

This document provides a guide on how to list all files and folders in a directory using Python's pathlib module. It explains the use of methods like .iterdir(), .rglob(), and .glob() for retrieving directory contents, including recursive listing and conditional filtering with glob patterns. Examples demonstrate how to implement these methods to access and display files and folders effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Getting a List of All Files and

Folders in a Directory in Python


 >>> import pathlib
 >>> desktop = pathlib.Path("C:/Users/RealPython/Desktop")
 >>> desktop
 WindowsPath("C:/Users/RealPython/Desktop")
Note:

 In the supplementary materials, you’ll find a folder called Desktop.


iterdir()
 It list the contents of a given directory, and you don’t need to get the
contents of each subdirectory too, then you can use the Path
object’s .iterdir() method.

 The .iterdir() method, when called on a Path object, returns a generator that
yields Path objects representing child items.

 If you wrap the generator in a list() constructor, then you can see your list of files and
folders
 >>> import pathlib
 >>> desktop = pathlib.Path("Desktop")

 >>> # .iterdir() produces a generator


 >>> desktop.iterdir()
 <generator object Path.iterdir at 0x000001A8A5110740>

 >>> # Which you can wrap in a list() constructor to materialize


 >>> list(desktop.iterdir())
 [WindowsPath('Desktop/Notes'),
 WindowsPath('Desktop/realpython'),
 WindowsPath('Desktop/scripts'),
 WindowsPath('Desktop/todo.txt')]
 >>> desktop = pathlib.Path("Desktop")
 >>> for item in desktop.iterdir():
 ... print(f"{item} - {'dir' if item.is_dir() else 'file'}")
 ...
 Desktop\Notes - dir
 Desktop\realpython - dir
 Desktop\scripts - dir
 Desktop\todo.txt - file
 >>> desktop = pathlib.Path("Desktop")
 >>> for item in desktop.iterdir():
 ... if item.is_file(): # if item.is_dir()

 ... print(item)
 ...
 Desktop\todo.txt
Recursively Listing With .rglob()

 To recursively list the items in a directory means to list not only the
directory’s contents, but also the contents of the subdirectories, their
subdirectories, and so on.
 >>> import pathlib
 >>> desktop = pathlib.Path("Desktop")

 >>> # .rglob() produces a generator too


 >>> desktop.rglob("*")
 <generator object Path.glob at 0x000001A8A50E2F00>

 >>> # Which you can wrap in a list() constructor to materialize


 >>> list(desktop.rglob("*"))
 [WindowsPath('Desktop/Notes'),
 WindowsPath('Desktop/realpython'),
 WindowsPath('Desktop/scripts'),
 WindowsPath('Desktop/todo.txt'),
 WindowsPath('Desktop/Notes/hash-tables.md'),
 WindowsPath('Desktop/realpython/iterate-dict.md'),
 WindowsPath('Desktop/realpython/tictactoe.md'),
 WindowsPath('Desktop/scripts/rename_files.py'),
 WindowsPath('Desktop/scripts/request.py')]
 The .rglob() method with "*" as an argument produces a generator that
yields all the files and folders from the Path object recursively
 Using a Python Glob Pattern for Conditional Listing

 A method related to .rglob() is the .glob() method. Both of these methods make use of glob
patterns. A glob pattern represents a collection of paths. Glob patterns make use of
wildcard characters to match on certain criteria.
Conditional Listing Using .glob()
 >>> import pathlib
 >>> desktop = pathlib.Path("Desktop")

 >>> # .glob() produces a generator too


 >>> desktop.glob("*")
 <generator object Path.glob at 0x000001A8A50E2F00>

 >>> # Which you can wrap in a list() constructor to materialize


 >>> list(desktop.glob("*"))
 [WindowsPath('Desktop/Notes'),
 WindowsPath('Desktop/realpython'),
 WindowsPath('Desktop/scripts'),
 WindowsPath('Desktop/todo.txt')]
 >>> desktop = pathlib.Path("Desktop")
 >>> list(desktop.glob("*.txt"))
 [WindowsPath('Desktop/todo.txt’)]

 to get only items that start with real


 >>> list(desktop.glob("real*"))
 [WindowsPath('Desktop/realpython’)]
You can also get the contents of a subdirectory by including its
name, a forward slash (/), and an asterisk

 >>> list(desktop.glob("realpython/*"))
 [WindowsPath('Desktop/realpython/iterate-dict.md'),
 WindowsPath('Desktop/realpython/tictactoe.md')]
Conditional Listing Using .rglob()

 >>> list(desktop.rglob("*.md"))
 [WindowsPath('Desktop/Notes/hash-tables.md'),
 WindowsPath('Desktop/realpython/iterate-dict.md'),
 WindowsPath('Desktop/realpython/tictactoe.md’)]

 >>> list(desktop.glob("**/*.md"))
 [WindowsPath('Desktop/Notes/hash-tables.md'),
 WindowsPath('Desktop/realpython/iterate-dict.md'),
 WindowsPath('Desktop/realpython/tictactoe.md')]
 >>> import pathlib
 >>> desktop = pathlib.Path("Desktop")

 >>> # Using a for loop


 >>> for item in desktop.rglob("*"):
 ... if item.is_file():
 ... print(item)
 ...
 Desktop\todo.txt
 Desktop\Notes\hash-tables.md
 Desktop\realpython\iterate-dict.md
 Desktop\realpython\tictactoe.md
 Desktop\scripts\rename_files.py
 Desktop\scripts\request.py
 >>> # Using a comprehension
 >>> [item for item in desktop.rglob("*") if item.is_file()]
 [WindowsPath('Desktop/todo.txt'),
 WindowsPath('Desktop/Notes/hash-tables.md'),
 WindowsPath('Desktop/realpython/iterate-dict.md'),
 WindowsPath('Desktop/realpython/tictactoe.md'),
 WindowsPath('Desktop/scripts/rename_files.py'),
 WindowsPath('Desktop/scripts/request.py')]

 >>> # Using the filter() function


 >>> list(filter(lambda item: item.is_file(), desktop.rglob("*")))
 [WindowsPath('Desktop/todo.txt'),
 WindowsPath('Desktop/Notes/hash-tables.md'),
 WindowsPath('Desktop/realpython/iterate-dict.md'),
 WindowsPath('Desktop/realpython/tictactoe.md'),
 WindowsPath('Desktop/scripts/rename_files.py'),
 WindowsPath('Desktop/scripts/request.py')]

You might also like