Temporary file

MRAB google at mrabarnett.plus.com
Thu Apr 9 20:29:33 EDT 2009


aditya shukla wrote:
> I am trying to create a temporary file.
> 
> import tempfile
> 
> temp = tempfile.NamedTemporaryFile(suffix='_suffix', 
> 
>                                    prefix='prefix_', 
>                                    dir='/tmp',
> 
>                                    )
> try:
>     print 'temp:', temp
> 
>     print 'temp.name <http://temp.name>:', temp.name
> finally:
> 
>     temp.close()
> 
> 
> But when i do ls /tmp or ls -a /tmp i cannot find the file or if i try to os.remove(temp.name <http://temp.name>)  it shows a message that the file is not found.But print temp and print temp.name <http://temp.name> show 
> 
> temp: <open file '<fdopen>', mode 'w+b' at 0xb7e84800>
> temp.name <http://temp.name>: /home/swamp2/cacsgrad/axs9347/Desktop/prefix_zOfoX__suffix
> 
> 
> 
> Please help me to fix it.
> 
> 
NamedTemporaryFile() can have an extra argument 'delete', which, if True
(the default), deletes the file after it's closed.

Try:

     temp = tempfile.NamedTemporaryFile(suffix='_suffix', 
prefix='prefix_', dir='/tmp', delete=False)


instead.

It's in the documentation:

http://docs.python.org/library/tempfile.html




More information about the Python-list mailing list