import tempfile
with tempfile.TemporaryFile() as tempf:
tempf.write(b"Hello World!") # write to the tempf
tempf.seek(0) # go to the first char of tempf
print(tempf.read()) # prints b'Hello World!'
import tempfile
# Creates a file and returns a tuple containing both the handle and the path.
handle, path = tempfile.mkstemp()
with open(handle, "w") as f:
f.write("Hello, World!");
import tempfile
with tempfile.TemporaryFile() as fp:
name = fp.name
fp.write("Hello World!") # Write a string using fp.write()
content = fp.read() # Read the contents using fp.read()
print(f"Content of file {name}: {content}")
print("File is now deleted")