Title: Create A Temporary File
Slug: create_a_temporary_file
Summary: Create A Temporary File Using Python.
Date: 2017-02-02 12:00
Category: Python
Tags: Basics
Authors: Chris Albon
In [1]:
from tempfile import NamedTemporaryFile
In [2]:
f = NamedTemporaryFile('w+t')
In [3]:
# Write to the file, the output is the number of characters
f.write('Nobody lived on Deadweather but us and the pirates. It wasn’t hard to understand why.')
Out[3]:
In [4]:
f.name
Out[4]:
In [5]:
# Go to the top of the file
f.seek(0)
# Read the file
f.read()
Out[5]:
In [6]:
f.close()