惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
云风的 BLOG
云风的 BLOG
小众软件
小众软件
雷峰网
雷峰网
博客园 - 【当耐特】
V
V2EX
WordPress大学
WordPress大学
IT之家
IT之家
Last Week in AI
Last Week in AI
罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
有赞技术团队
有赞技术团队
The Cloudflare Blog
Jina AI
Jina AI
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
博客园 - 聂微东
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

alexwlchan’s notes

What is WS11 1DB? Blocking referrers with Caddy How to type a Spanish question mark (¿) on a Mac Non-overlapping type comparisons and Python type checkers Why does t.Setenv panic after t.Parallel? Use Path.glob() and Path.rglob() for typed versions of glob.glob() Curious clocks and colourful eyes Track which templates are used by Jinja2 Archeologists distinguish between “sherds” and “shards” A single command to test all my changed Go packages Disable the new message animations in WhatsApp Finding high-churn folders that bother Backblaze Always-on SSH agent forwarding with my Git pushes Managing the caption of a photo with AppleScript (but not PhotoKit) Goodhart’s and Campbell’s Law are different Notes from The Cornishman No. 176 (Spring 2026) Notes from The Cornishman No. 176 (Spring 2026) GitUp can’t diff text files larger than 8MB Home Testing the width of a page on a mobile device using Playwright Disable AirPods charging notifications Start a Caddy server in a subprocess during a Python session Filter a list of JSON object based on a list of tags HOME_GET_ME_HOME is a Citymapper Shortcuts action The red-lined bubble snail Why can’t Python connect to example.com? Useful type hints for Python How to truncate the middle of long command output AirPlay Receiver can interfere with Flask apps What’s the main prefix in SQLite queries?
The FileExistsError exception exposes a filename attribute
2026-04-08 · via alexwlchan’s notes

The filename attribute returns the name of the already-existent file as a string.

Here’s a simple snippet that shows it returning a string attribute:

try:
    with open("greeting.txt", "x") as out_file:
        out_file.write("hello world!")
except FileExistsError as err:
    print(repr(err.filename))  # 'greeting.txt'

It’s not especially useful in this example because you already know the name of the file you’re writing – but I’ve found it useful with the download_image() function I wrote for chives.fetch.

The download_image() function takes a URL and an out prefix, looks at the Content-Type header of the response to decide the file extension, and returns the downloaded path. I don’t know what path it will use until I’ve got the server response.

The function won’t overwrite existing images, so it throws a FileExistsError exception if the image already exists. If I want to carry on anyway with the already-downloaded image, I can get the filename from the exception rather than re-calculating the filename or changing the API.

Here’s another example:

from chives.fetch import download_image
from pathlib import Path

try:
    out_path = download_image(
        url="https://alexwlchan.net/images/2026/470906.png",
        out_prefix=Path("example")
    )
    print(out_path)  # Path("example.png")
except FileExistsError as err:
    print(repr(err.filename))  # 'example.png'

The filename attribute comes from OSError, of which FileExistsError is a subclass.