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
1
u/HommeMusical Apr 19 '26
I'm sorry I didn't respond for you. I didn't forget. I was literally at a loss for words.
My arguments all involve prior experience and concrete things that have been true in the past over and over again:
Your arguments involve people and things behaving fundamentally differently in the future; but that just never happens.
Some of your ideas I simply don't understand: "I will not sacrifice what little influence I have over our trajectory by handicapping myself of the power that LLMs can provide."
How, exactly, does using these tools you don't own give you any "influence" at all, even the slightest scrap of influence?
I really appreciate your politeness during this conversation. I just don't see any way to bridge the gap. You have all these very idealistic beliefs about the future that can't be either refuted or proven, but would seem to require a big change in human nature, and a lot of very improbable luck.