Is there a way to "lazy" open a file in Python?
If I do Path("my_path").open("wb")
, the file will be created and be empty until I write on it. What I want is for the file to not be created until I actually call the write() function on it.
In my specific case, I am opening a streaming request and heuristically determining the filetype from the first chunk, and if I don't open the file before iterating through the chunks I'll have to open and close the handler continuously. Right now I have to unlink the file and open a separate one if I determine that the url's extension is wrong, but I was looking for a more elegant solution.