0

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.

Some Guy
  • 576
  • 1
  • 4
  • 17
  • 1
    Sure you could write e.g. a file wrapper that does that, but there's no built-in lazy opening in Python. – AKX Nov 21 '21 at 17:02
  • 1
    You could check if `os.path.exists("my_path")` and then open file if it true. Or you can check `Path("my_path").exists()` – John Byro Nov 21 '21 at 17:05
  • 3
    It's not really clear what problem you hope to solve this way. How long are you planning to wait? Why not just open the file at the time that you need to start writing? It seems like you want to pass around an object that represents an unopened file - why not just pass around the `Path`? – Karl Knechtel Nov 21 '21 at 17:05
  • Why not opening just before `write `? – hpchavaz Nov 21 '21 at 17:06
  • 1
    My scenario is the following: I open a requests.get(url, stream=True) object and [write the chunks to a file](https://stackoverflow.com/questions/16694907), however I do not know the correct filetype for sure beforehand. I would like to analyze the first chunk with a mime library, and if the actual filetype is different (for example, a png served as a jpg) then I want to rename the destination and save to there instead. The file must be open before I start looping around the chunks, otherwise I need to open/close at every chunk. Right now I'm forced to also delete the destination. – Some Guy Nov 21 '21 at 17:14
  • Create an object that looks and acts like a file but only does the open later. There's nothing of the sort built-in, but it's easy enough to write one yourself. – Charles Duffy Nov 21 '21 at 17:16
  • 3
    @ihatenginx You could have added that to your original post! :) You could start writing chunks to an `io.BytesIO()`, and when you have enough to write to disk, open the file, use `shutil.copyfileobj()` to write the bytesio's contents there and keep writing to the disk? – AKX Nov 21 '21 at 17:18
  • BTW, I agree with AKX that the description of the underlying use case would be better off as part of the question than in a comment. – Charles Duffy Nov 21 '21 at 17:19
  • I've updated the question with the use case. Can I drop the first chunks to the bytesio object, and then once i'm done determining the extension, dump the object to a file and resume writing the rest of the chunks to the file? because I'd accept that as a solution. – Some Guy Nov 21 '21 at 17:52

0 Answers0