Skip to content

Instantly share code, notes, and snippets.

@nagos
Created October 26, 2022 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nagos/57a1dca44073268b4287791677c66cc4 to your computer and use it in GitHub Desktop.
Save nagos/57a1dca44073268b4287791677c66cc4 to your computer and use it in GitHub Desktop.
Python dir listing without recursive functions
#!/usr/bin/env python3
import os
root_path = "/"
path_list = [root_path]
while len(path_list):
path = path_list.pop()
try:
with os.scandir(path) as it:
for entry in it:
fullname = path+entry.name
if not entry.name.startswith('.') and entry.is_file():
print(f"File: {fullname}")
if not entry.name.startswith('.') and entry.is_dir():
print(f"Directory: {fullname}")
path_list.append(fullname + "/")
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment