Temporary file

Dave Angel davea at ieee.org
Fri Apr 10 00:43:38 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:', 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)  it shows a message that the file is not
> found.But print temp and print temp.name show
> temp: <open file '<fdopen>', mode 'w+b' at 0xb7e84800>
> temp.name: /home/swamp2/cacsgrad/axs9347/Desktop/prefix_zOfoX__suffix
>
>
>
> Please help me to fix it.
>
>
>
> Aditya
>
>   
According to the docs for NamedTemporaryFile(), the file will be deleted 
when closed (unless you specify otherwise with delete=False)




More information about the Python-list mailing list