#!/bin/python3 """ Prompt: The script will accept a single command-line argument, which is intended to be a path to a directory. For the given directory, and each of the directories contained within it recursively, print the path to the directory and a list of the regular files in it. Note the following requirements: - Within a directory, list files in descending numeric order by file size. - For each file, print the file name, size, and last modification timestamp. - Format the output clearly. (See below for one possible format.) - You may not use the ls program (or similar) at all in your solution. Example output: ``` show-dirs-files gadgets Directory 5120 1982-03-02 03:14:00 whip 4608 1982-02-02 03:14:00 frappe 4096 1982-09-02 03:14:00 fluff 3584 1982-08-02 03:14:00 cream 3072 1982-01-02 03:14:00 beat 2560 1982-07-02 03:14:00 combine 2048 1982-06-02 03:14:00 blend 1536 1982-05-02 03:14:00 mix 1024 1982-04-02 03:14:00 stir 512 1983-01-02 03:14:00 fold Directory 2560 2010-09-07 08:15:00 avalanche 2048 2010-10-07 08:19:00 malibu 1536 2010-09-07 08:18:00 tahoe 1024 2010-09-07 08:16:00 camaro 512 2010-09-07 08:17:00 agile 512 2010-09-07 08:19:00 prism [...] ``` """ import os import sys from datetime import datetime def recursive_list(directory): if not os.path.isdir(directory): return elif directory[-1] == "/": directory = directory[:-1] contents = [] for item in os.listdir(directory): path = f"{directory}/{item}" if os.path.isdir(path): recursive_list(path) else: # Print the file name, size, and last modification timestamp mdate = datetime.fromtimestamp(os.path.getmtime(path)) fsize = os.path.getsize(path) contents.append((fsize, mdate, path)) if contents: print(f"\nDirectory <{directory}>") for fsize, mdate, path in sorted(contents, key=lambda ctup: ctup[0], reverse=True): print(f"{fsize:>12} {mdate} {path}") if __name__ == "__main__": recursive_list(*sys.argv[1:])