DeaD_EyE

sorted files and folders

Apr 25th, 2019
101
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from pathlib import Path
  2.  
  3.  
  4. def sort_files(folders, files):
  5.     def sorter(path):
  6.         return str(path).lower()
  7.     folder_content = sorted(folders, key=sorter)
  8.     folder_content += sorted(files, key=sorter)
  9.     return folder_content
  10.    
  11.  
  12. def get_content(path):
  13.     path = Path(path)
  14.     folders = []
  15.     files = []
  16.     for file in path.iterdir():
  17.         if file.is_dir():
  18.             folders.append(file)
  19.         elif file.is_file():
  20.             files.append(file)
  21.     return sort_files(folders, files)
  22.  
  23.  
  24. sorted_folder = get_content(Path.home() / 'Downloads')
RAW Paste Data