From 4b8a8f006c706d1680e99f6b6b6d7e0c8fd33472 Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Fri, 12 Jan 2024 10:56:00 +0000 Subject: [PATCH] add dirtrav --- chapter7/dirtrav.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 chapter7/dirtrav.py diff --git a/chapter7/dirtrav.py b/chapter7/dirtrav.py new file mode 100644 index 0000000..b200c9a --- /dev/null +++ b/chapter7/dirtrav.py @@ -0,0 +1,27 @@ +import logging +from collections import deque +from pathlib import Path + +logging.basicConfig(level=logging.DEBUG) +logger = logging.getLogger(__name__) + +scripts_path = Path.home() / "scripts" + + +def files_with_extension(ext): + queue = deque() + queue += scripts_path.glob("*") + + while queue: + item = queue.popleft() + + # if it is a file and has extension ext then print + if item.is_file() and item.suffix == ext: + print(item) + + # otherwise add directory items to the queue + else: + queue += item.glob("*") + + +files_with_extension(".sh")