file tempfile

file tempfile, introduced by TIP # 210 in Tcl 8.6, creates a temporary file and returns an open read-write channel to it.

See Also

Creating Temporary Files
A summary of the options for creating temporary files, along with some routines to do the job.


file tempfile ?varName? ?template?


file tempfile finds an unused file name using template if given, creates a file by that name, returns an open channel to the file, and attempts to delete the file when it is closed. If varname is given, stores the name of the created file in the variable having that name and does not attempt to delete the file when it is closed. template may include directory components and a file name extension.

when varname is used, it is the responsibility of the caller to delete the file when it is no longer needed. Rather than usin try... finally for cleanup one trick is to delete the file immediately after creating it. The channel remains available until closed but the file is no longer visible in the filesystem:

set chan [file tempfile filename]
file delete $filename
do stuff with $chan
close $chan


% # <directory><prefix><extension>
% file tempfile x /tmp/dingo.txt
% set x
% # <prefix><extension> 
% file tempfile x dingo.txt
% set x
%# <prefix>
% file tempfile x txt
% set x
%# <extension>
% file tempfile x .txt
% set x
% # Cant use directory by itself as shown below it takes the directory as / and prefix as tmp
% file tempfile x /tmp 
can't create temporary file: permission denied

AMG: The template filename extension is ignored in Windows [L1 ]. This is a problem because Windows uses extensions to determine how to "open" files.

Page Authors