Skip to main content

fs/latest_download

1#!/usr/bin/env python3
2"""
3This script prints the path to the newest file in my Downloads folder.
5I use it when I've downloaded a file in my web browser, and I want
6to use that file immediately without checking what its filename is.
7"""
9import datetime
10import os
13def get_downloads():
14 downloads_dir = os.path.join(os.environ["HOME"], "Downloads")
16 for f in os.listdir(downloads_dir):
17 p = os.path.join(downloads_dir, f)
19 if not os.path.isfile(p):
20 continue
22 if f == ".DS_Store":
23 continue
25 yield p
28def downloaded_time(p):
29 return os.stat(p).st_mtime
32if __name__ == "__main__":
33 print(max(get_downloads(), key=lambda p: downloaded_time(p)))