[Tutor] Can tempfile.NamedTemporaryFile(delete=False) be used to create *permanent* uniquely named files?

Cameron Simpson cs at cskk.id.au
Sun Oct 21 04:31:13 EDT 2018

On 21Oct2018 01:13, boB Stepp <robertvstepp at gmail.com> wrote:
>Use case:  I want to allow a user of my Solitaire Scorekeeper program
>to be able to give any name he wants to each game of solitaire he
>wishes to record.  My thought for permanent storage of each game's
>parameters is to use a dictionary to map the user-chosen game names to
>a unique json filename.  This way I hope no shenanigans can occur with
>weird characters/non-printing characters.
>My initial thought was to just have a sequence of game names with
>incrementing numerical suffixes:  game_0, game_1, ... , game_n.  But
>this would require the program to keep track of what the next
>available numerical suffix is.  Additionally, if a user chooses to
>delete a game, then there would be a gap in the numerical sequence of
>the game filenames.  I find such a gap aesthetically displeasing and
>would probably go to additional efforts to reuse such deleted
>filenames, so there would be no such "gaps".
>So I am now wondering if using
>tempfile.NamedTemporaryFile(delete=False) would solve this problem
>nicely?  As I am not very familiar with this library, are there any
>unforeseen issues I should be made aware of?  Would this work equally
>well on all operating systems?

The doco reads that way to me.

However, NamedTemporaryFile is a (nice) wrapper for tempfile.mkstemp().  
Why not use that directly?

Cameron Simpson <cs at cskk.id.au>

More information about the Tutor mailing list