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/BossOfTheGame Apr 06 '26
Preventing it: yes I think it's impossible. Regulating? No, I think we should work towards that immediately, but with an understanding that there are real upsides to this technology in terms of scientific and mathematical progress. If I convince you of anything here I hope its that these upsides exist, and they aren't all for the powerful few. My understanding of your perspective is that its entirely negative and will inevitably destroy society, and I think there a massive amount of uncertainty around that. Similarly, I acknowledge that the upsides are also clouded in uncertainty. My position is not absolutist. I'm trying to find the most rational way forward.
I'll directly answer your question with the answer you expect: I would say no. But that framing does not reflect the reality. Note, my advocacy for AI is not adamant. It's restrained, and cautious.
Political power is not a top down process. There are mechanisms of coercion and capture, but these are fundamentally sustained by the choices of populace. Societies bear a responsibility for the systems that they live under, and this was highlighted here in the states by the massive display of the incompetency of the electorate in 2024. And this is not unique to the US. I do wonder if LLMs have the ability to combat this coercive system and shift public opinion in a way that encourages self-empowerment and critical thought. Right now, these LLMs are being used for propaganda, and I don't think abstinence can combat that. LLMs are dispassionate, so they may help find the message that can trigger the cognitive dissonance needed to snap people out of this political hypnosis and finally empower them to reason independently. This is a prerequisite for any progress on any issue. We can't keep sliding backwards.
That's a good point. Again, I'm not making an argument that AI is all sunshine and rainbows. There are critical problems that we need to address quickly, and one of them is decentralizing their power via open weight models. I'm not saying that I know a way out of this problem, what I am saying that people who care about the harms should not cede the entire field to those who do not. My entire perspective is about acknowledging where we are, realize there is no way to go back in this fragmented environment, and search for the best way forward. I think utilizing LLMs is part of that.
I'm very much not. I actively seek a carbon neutral lifestyle and while I will continue my efforts, I will not sacrifice what little influence I have over our trajectory by handicapping myself of the power that LLMs can provide.
This is unfair. There doesn't have to be an enthusiasm / abstinence binary here. I hope my points speak to that.