Created
October 26, 2022 13:52
-
-
Save nagos/57a1dca44073268b4287791677c66cc4 to your computer and use it in GitHub Desktop.
Python dir listing without recursive functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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