Posts about tempfile

Create A Temporary File

Goal

This post aims to create a temporary file using tempfile

Reference

Libraries

In [1]:
import tempfile

Create a temporary file

In [9]:
temp_file = tempfile.NamedTemporaryFile()
temp_file.name
Out[9]:
'/var/folders/9_/tl0k78wd62xchzsh_4x4bz0h0000gn/T/tmp75145s92'
In [23]:
!ls -a /var/folders/9_/tl0k78wd62xchzsh_4x4bz0h0000gn/T/ | grep tmp7
tmp75145s92

Createa a temporary directory

In [8]:
temp_dir = tempfile.TemporaryDirectory()
temp_dir.name
Out[8]:
'/var/folders/9_/tl0k78wd62xchzsh_4x4bz0h0000gn/T/tmpje1mz_wj'
In [19]:
!ls /var/folders/9_/tl0k78wd62xchzsh_4x4bz0h0000gn/T/ | grep tmpje
tmpje1mz_wj