import tempfile with tempfile.NamedTemporaryFile(mode='w', prefix='example', suffix='.txt', delete=False) as temp_file: temp_file.write('Hello World!') temp_file.seek(0) print(temp_file.read())
import tempfile with tempfile.NamedTemporaryFile(mode='w', delete=False) as temp_file: temp_file.write('Hello World!') temp_file.seek(0) print(temp_file.read())In this example, the file name is not specified and will be randomly generated. All other arguments remain the same as in the previous example. The 'tempfile' package is a built-in package in Python and does not require any installation.