August 2018
Intermediate to advanced
366 pages
10h 14m
English
tempfile.NamedTemporaryFile will create the temporary file:
>>> from tempfile import NamedTemporaryFile >>> >>> with tempfile.NamedTemporaryFile() as f: ... print(f.name) ... /var/folders/js/ykgc_8hj10n1fmh3pzdkw2w40000gn/T/tmponbsaf34
The fact that the .name attribute leads to the full file path on disk allows us to provide it to other external programs:
>>> with tempfile.NamedTemporaryFile() as f:
... os.system('echo "Hello World" > %s' % f.name)
... f.seek(0)
... print(f.read())
...
0
0
b'Hello World\n'