Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- files_by_type = {}
- # for item in os.listdir('.'): # goes in current level only
- # if os.path.isfile(os.path.join('.', item)):
- level_count = 0
- for _, _, file_names in os.walk("."): # goes down to the bottom
- for item in file_names:
- extension = '.' + item.split('.')[-1]
- if extension not in files_by_type:
- files_by_type[extension] = []
- files_by_type[extension].append(item)
- level_count += 1
- if level_count == 2: # goes one level deeper and stops, we can control how deep the search goes.
- break
- desktop_dir = os.path.normpath(os.path.expanduser("~/Desktop"))
- path_file = os.path.join(desktop_dir, 'report.txt')
- with open(path_file, 'w') as output_file:
- for ext, f_names in sorted(files_by_type.items()):
- output_file.write(f"{ext}\n")
- for name in sorted(f_names):
- output_file.write(f"- - - {name}\n")
Advertisement
Add Comment
Please, Sign In to add comment