Today we will learn a simple technique for handling temporary files in Python. We face some situations where we need temporary files in our programs. Usually what we do is create a file and then do the stuff then after done we programmatically delete the file. Today we are going to learn how to create a temporary file which automatically deleted when we close the file. In python’s standard library then is a module called tempfile. Let’s learn with an example
import tempfile with tempfile.TemporaryFile() as fp: fp.write(b'Hello world!') fp.seek(0) fp.read()
We can see that a tempfile works similarly to a regular file in python. This module has a temporary directory feature as well. To learn more deeply you can refer to this link tempfile.
I hope you have learned something from this post. Please share your valuable suggestion with email@example.com