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 os
12def get_downloads():
13 downloads_dir = os.path.join(os.environ["HOME"], "Downloads")
15 for f in os.listdir(downloads_dir):
16 p = os.path.join(downloads_dir, f)
18 if not os.path.isfile(p):
19 continue
21 if f == ".DS_Store":
22 continue
24 # Exclude partially downloaded files from Firefox
25 if f.endswith(".part"):
26 continue
28 yield p
31if __name__ == "__main__":
32 print(max(get_downloads(), key=lambda p: os.stat(p).st_mtime))