1

I need to create a NamedTemporaryFile with a custom name.

I tried setting the name attribute but that did not work.

from tempfile import NamedTemporaryFile

f = NamedTemporaryFile(dir='/tmp/')
f.name = custom_name

The file name does not change when I tried with os.path.exists, it's returning true for the old name.

I have already viewed temp files/directories with custom names?, but creating a temporary directory does not fit my use case.

LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114
traintraveler
  • 351
  • 2
  • 9
  • 1
    Short answer: It's not possible. Why don't you just `open()` a file in `os.environ.get("TMPDIR") or "/tmp/"`? – L3viathan Aug 14 '19 at 12:42
  • I might need it to create like around 500 - 1000 files in some use-cases, what would be a better approach using os.link to create a hard link to this temporary file and deleting the temp file or by just using open() ? – traintraveler Aug 15 '19 at 14:54
  • What's the downside of using `open()`? – L3viathan Aug 15 '19 at 16:22
  • I thought it would be convenient if Python managed the removal of the file, but os.link (nullifies it) seems to do the same as that of open() with the overhead of creating a tempfile and linking to it. Thank you, I'll go with open() :) – traintraveler Aug 15 '19 at 16:55

1 Answers1

2

There is no name parameter for NamedTemporaryFile. The names are generated by tempfile._get_candidate_names. It would be possible to monkey patch this and provide your own name. You can add a prefix and a suffix to the generated name however, making at least part of the name predictable:

Python 3.6.9 (default, Nov 23 2019, 06:49:55) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from tempfile import NamedTemporaryFile                                                                                                                                                                                     

In [2]: file = NamedTemporaryFile(prefix='asdf_', mode='w+', suffix='.xlsx')                                                                                                                                                        

In [3]: file.name                                                                                                                                                                                                                   
Out[3]: '/tmp/asdf_uskygtov.xlsx'

Here is an example of the monkey patch:

Python 3.6.9 (default, Nov 23 2019, 06:49:55) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import tempfile                                                                                                                                                                                                             

In [2]: import itertools                                                                                                                                                                                                            

In [3]: tempfile._get_candidate_names = lambda: itertools.repeat('my_file')                                                                                                                                                         

In [4]: file = tempfile.NamedTemporaryFile(mode='w+', prefix='', suffix='txt')                                                                                                                                                      

In [5]: file.name                                                                                                                                                                                                                   
Out[5]: '/tmp/my_filetxt'

In [6]: file = tempfile.NamedTemporaryFile(mode='w+', prefix='', suffix='.txt')                                                                                                                                                     

In [7]: file.name                                                                                                                                                                                                                   
Out[7]: '/tmp/my_file.txt'
Bemis
  • 3,092
  • 2
  • 17
  • 24