Hey guys.
So I was working on this task where I want a user to input a path of their choice so the program can interate through the directory and print all files with their extensions.
This is the code:
from collections import defaultdict
from pathlib import Path
def validate_path():
while True:
path = Path(input("Enter the path: "))
if not path.is_dir():
print("Please enter a valid path.")
else:
return path
def group_items(path):
file_groups = defaultdict(list)
for item in path.iterdir():
if not item.is_file():
continue
extension = item.suffix.casefold()
file_groups[extension].append(item)
return file_groups
def print_groups(file_groups):
for extension, files in sorted(file_groups.items()):
print(f"{extension}: {len(files)}")
for file in sorted(files):
print(f" {file.name}")
def main():
path = validate_path()
file_groups = group_items(path)
print_groups(file_groups)
if __name__ == "__main__":
main()
My goal now is to learn more and more fundamentals of python/programming by adding more stuff to this code.
Examples:
- Try/Except blocks.
- Text Editing
- Reading / Writing files
- Regex function to maybe open a file and filter through "err*" text.
It would be nice if an experienced person could give me "tasks" to add to this code rather than me going ahead and asking ai "what more can i do?" or "give me more tasks".
One task f.e. could be "add a ... here and then make sure that .... is being checked" or something like that.
Does that make sense?
Would be super helpful!