import tempfile with tempfile.NamedTemporaryFile(mode='w', delete=False) as temp: temp.write('This is a temporary file\n') temp.write('It is deleted automatically when closed\n') print('Temporary file created at', temp.name) # At this point, the temporary file is automatically closed and deletedIn this example, a temporary file is created with `delete=False` to ensure that it is not deleted automatically when closed. The `mode` parameter specifies that the file should be opened in write mode (`'w'`). The file object `temp` is used to write some text to the file, and the `print` statement displays the filename of the temporary file that was created. The `with` statement ensures that the file is automatically closed and deleted when the statements within the block are finished executing. The `tempfile` module is part of Python's standard library, so no additional package installation is required.