Python Temporary Files
I always forget basic usage of the tempfile module in python, so I made a summary of what I typically use. As it turns out some basic knowledge of filehandles and the os module may also help.
- Official Python Documentation on the tempfile module: [1]
- Official Python Documentation on the os module: [2]
NamedTemporaryFile
From the Python documentation:
NamedTemporaryFile( [mode="w+b"[, bufsize=-1[, suffix=""[, prefix="tmp"[, dir=None[, delete=True]]]]]])
[...] the file is guaranteed to have a visible name in the file system [...] can be retrieved from the name attribute of the file object. [...] If delete is true [...] deleted as soon as it is closed.
A short example of how you typically use a NamedTemporaryFile - with no special parameters. Note that the file is deleted upon close.
>>> from tempfile import NamedTemporaryFile >>> from os.path import exists >>> ntf = NamedTemporaryFile() >>> ntf.name '/tmp/tmpGFJp4a' >>> exists( ntf.name ) True >>> ntf.write( "it's not dead it's just resting" ) >>> ntf.seek( 9 ) >>> ntf.read( 4 ) 'dead' >>> ntf.close() >>> exists( ntf.name ) False
A slightly longer example where the user has to manually delete (aka "unlink") the file.
>>> from tempfile import NamedTemporaryFile >>> from os.path import exists >>> from os import unlink >>> ntf = NamedTemporaryFile( suffix=".tmp", prefix="tmp_", delete=False ) >>> ntf.write( 'foo' ) >>> ntf.seek( 1 ) >>> ntf.read() 'oo' >>> name = ntf.name >>> name '/tmp/tmp_wFNKmo.tmp' >>> ntf.close() >>> exists( name ) True >>> unlink( name ) >>> exists( name ) False
mkstemp
Again, from the Python documentation:
mkstemp( [suffix=""[, prefix="tmp"[, dir=None[, text=False]]]])
Creates a temporary file in the most secure manner possible. [...] Unlike TemporaryFile(), the user of mkstemp() is responsible for deleting the temporary file when done with it. [...] mkstemp() returns a tuple containing an OS-level handle to an open file (as would be returned by os.open()) and the absolute pathname of that file [...]
>>> from tempfile import mkstemp >>> from os.path import exists >>> from os import unlink, close, write >>> ( handle, name ) = mkstemp( suffix = ".data", prefix = "sensor_" ) >>> type( handle ) <type 'int'> >>> type( name ) <type 'str'> >>> name '/tmp/sensor_Cmdtrk.data' >>> write( handle, 'foobie bletch' ) 13 >>> close( handle ) >>> exists( name ) True >>> f = open( name, 'r') >>> f.read() 'foobie bletch' >>> f.close() >>> exists( name ) True >>> unlink( name ) >>> exists( name ) False
Just a random temporary filename
This could be a start if you just want a random temporary filename.
>>> from tempfile import gettempdir, gettempprefix >>> from os.path import join >>> join( gettempdir(), gettempprefix() ) '/tmp/tmp'
Or perhaps something longer like this:
>>> from tempfile import gettempdir, gettempprefix >>> from os.path import join >>> from string import ascii_lowercase, digits >>> from random import choice >>> >>> ( suffix1, suffix2 ) = ( "", "" ) >>> >>> for _ in xrange( 8 ): ... suffix1 += choice( ascii_lowercase ) ... suffix2 += choice( digits ) ... >>> print join( gettempdir(), '-'.join( [ gettempprefix(), suffix1, suffix2 ] ) ) /tmp/tmp-bsanoyzp-53044506
But remember that there is no guarantee that this filename is unique, so I would use some checks to see that the filename is ok before I started using it.
Tillhör Kategori Programmering