#!/usr/bin/env python3 """ This script prints the path to the newest file in my Downloads folder. I use it when I've downloaded a file in my web browser, and I want to use that file immediately without checking what its filename is. """ import datetime import os def get_downloads(): downloads_dir = os.path.join(os.environ["HOME"], "Downloads") for f in os.listdir(downloads_dir): p = os.path.join(downloads_dir, f) if not os.path.isfile(p): continue if f == ".DS_Store": continue yield p def downloaded_time(p): return os.stat(p).st_mtime if __name__ == "__main__": print(max(get_downloads(), key=lambda p: downloaded_time(p)))