I have a project where I need to create temp files.
I have a class that accepts a file path. and as needed it reads the data from the file. However, sometimes I have the data and I want to create the same object so I use tempfile
. But I want the file to be deleted as the object garbage-collected.
This is my implementation which is wrong.
from pickle import load
import tempfile
class A:
def __init__(self, file_path):
self.file_path = file_path
@property
def data(self):
with open(self.file_path, "rb") as f2r:
return load(f2r)
@classmethod
def from_data(cls, data):
with tempfile.NamedTemporaryFile() as tmp:
tmp.write(data)
return cls(tmp.name)
The from_data
method creates the temp file and returns an object but the file will be deleted as soon as from_data
returns the object. But I want the file to live until the object itself is garbage-collected.
As @Nick ODell suggested I am going to use __del__
method to delete the file on garbage-collection.
So the code would look like:
from pathlib import Path
from pickle import load
import tempfile
class A:
def __init__(self, file_path):
self.file_path = file_path
self.is_temp = False
def __del__(self):
if self.is_temp:
Path(self.file_path).unlink()
@property
def data(self):
with open(self.file_path, "rb") as f2r:
return load(f2r)
@classmethod
def from_data(cls, data):
with tempfile.NamedTemporaryFile(delete=False) as tmp:
tmp.write(data)
a = cls(tmp.name)
a.is_temp = True
return a