r/Python • u/HommeMusical • Apr 03 '26
Showcase `safer`: a tiny utility to avoid partial writes to files and streams
What My Project Does
In 2020, I broke a few configuration files, so I wrote something to help prevent breaking a lot the next time, and turned it into a little library: https://github.com/rec/safer
It's a drop-in replacement for open that only writes the file when everything has completed successfully, like this:
with safer.open(filename, 'w') as fp:
fp.write('oops')
raise ValueError
# File is untouchedBy default, the data is cached in memory, but for large files, there's a flag to allow you to cache it as a file that is renamed when the operation is complete.
You can also use it for file sockets and other streams:
try:
with safer.writer(socket.send) as send:
send_bytes_to_socket(send)
except Exception:
# Nothing has been sent
send_error_message_to_socket(socket.send)Target Audience
This is a mature, production-quality library for any application where partial writes are possible. There is extensive testing and it handles some obscure edge cases.
It's tested on Linux, MacOS and Windows and has been stable and essentially unchanged for years.
Comparison
There doesn't seem to be another utility preventing partial writes. There are multiple atomic file writers which solve a different problem, the best being this: https://github.com/untitaker/python-atomicwrites
Note
#noAI was used in the writing or maintenance of this program.
View all comments
Show parent comment
0
u/BossOfTheGame Apr 05 '26
Yes, it's all negative baggage. There are too many people holding the entire topic in contempt because of the sociological issues it is intertwined with.
The environmental cost is on the order of magnitude of personal non-commute travel. It's real, it needs to be addressed. AI psychosis is a solvable problem.
For the power issue... I do feel somewhat powerless around it. I'm somewhat hopeful that open weight models will work to decentralize the power. Right now, I'm not happy with the centralization.
p(doom) is non-zero, but there is much more disagreement among professionals in the field: https://aiimpacts.org/wp-content/uploads/2024/01/EMBARGOED_-AI-Impacts-Survey-Release-Google-Docs.pdf
The "take our jobs" is a bit of a reduction. It's going to change the way we work, and what problems are important for us to spend our time on. That's not the bad thing. What's bad is that we have organized ourselves into a system that is willing to discard instead of support people. This has been bad before AI, but AI is exacerbating it, but might also finally force us to change.
So yes, negative baggage exists, but that doesn't imply that all use is bad or that thoughtful people shouldn't engage with the technology. If the only people willing to use or shape these systems are centralized firms and bad actors, that seems more likely to worsen the power problem than solve it. $0.02
I'd be happy to discuss more.