[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Understanding tempfile.TemporaryFile

byte8bits

12/28/2007 2:49:00 AM

Wondering if someone would help me to better understand tempfile. I
attempt to create a tempfile, write to it, read it, but it is not
behaving as I expect. Any tips?

>>> x = tempfile.TemporaryFile()
>>> print x
<open file '<fdopen>', mode 'w+b' at 0xab364968>
>>> print x.read()

>>> print len(x.read())
0
>>> x.write("1234")
>>> print len(x.read())
0
>>> x.flush()
>>> print len(x.read())
0
7 Answers

John Machin

12/28/2007 3:13:00 AM

0

On Dec 28, 1:49 pm, byte8b...@gmail.com wrote:
> Wondering if someone would help me to better understand tempfile. I
> attempt to create a tempfile, write to it, read it, but it is not
> behaving as I expect. Any tips?
>
> >>> x = tempfile.TemporaryFile()
> >>> print x
>
> <open file '<fdopen>', mode 'w+b' at 0xab364968>
>
> >>> print x.read()
> >>> print len(x.read())
> 0
> >>> x.write("1234")
> >>> print len(x.read())
> 0
> >>> x.flush()
> >>> print len(x.read())
>
> 0

This is nothing particular to your subject; it applies to all files.

x.read() starts reading at the CURRENT POSITION, not at the start of
the file. In all cases above, the then current position was the END of
the file, so x.read() returned a zero-length string.

Check out the seek method.

Shane Geiger

12/28/2007 3:17:00 AM

0

import tempfile
tmp = tempfile.mktemp()

import os
os.remove(tmp)



byte8bits@gmail.com wrote:
> Wondering if someone would help me to better understand tempfile. I
> attempt to create a tempfile, write to it, read it, but it is not
> behaving as I expect. Any tips?
>
>
>>>> x = tempfile.TemporaryFile()
>>>> print x
>>>>
> <open file '<fdopen>', mode 'w+b' at 0xab364968>
>
>>>> print x.read()
>>>>
>
>
>>>> print len(x.read())
>>>>
> 0
>
>>>> x.write("1234")
>>>> print len(x.read())
>>>>
> 0
>
>>>> x.flush()
>>>> print len(x.read())
>>>>
> 0
>


--
Shane Geiger
IT Director
National Council on Economic Education
sgeiger@ncee.net | 402-438-8958 | http://ww...

Leading the Campaign for Economic and Financial Literacy

Steven D'Aprano

12/28/2007 3:27:00 AM

0

On Thu, 27 Dec 2007 18:49:06 -0800, byte8bits wrote:

> Wondering if someone would help me to better understand tempfile. I
> attempt to create a tempfile, write to it, read it, but it is not
> behaving as I expect. Any tips?

You need to seek to the part of the file you want to read:

>>> x = tempfile.TemporaryFile()
>>> x.read() # file is empty to start with
''
>>> x.write('Nobody expects the Spanish Inquisition!')
>>> x.read() # current file is at the end of the file, so nothing to read
''
>>> x.seek(0) # move to the beginning of the file
>>> x.read()
'Nobody expects the Spanish Inquisition!'
>>> x.seek(7)
>>> x.write('EXPECTS')
>>> x.tell() # where are we?
14L
>>> x.seek(0)
>>> x.read()
'Nobody EXPECTS the Spanish Inquisition!'


--
Steven

byte8bits

12/28/2007 3:29:00 AM

0

On Dec 27, 10:12 pm, John Machin <sjmac...@lexicon.net> wrote:

> Check out the seek method.

Ah yes... thank you:

>>> import tempfile
>>> x = tempfile.TemporaryFile()
>>> x.write("test")
>>> print x.read()

>>> x.seek(0)
>>> print x.read()
test


Steven D'Aprano

12/28/2007 3:36:00 AM

0

On Thu, 27 Dec 2007 21:17:01 -0600, Shane Geiger wrote:

> import tempfile
> tmp = tempfile.mktemp()
>
> import os
> os.remove(tmp)

Not only does that not answer the Original Poster's question, but I don't
think it does what you seem to think it does.


>>> tmp = tempfile.mktemp()
>>> tmp
'/tmp/tmpZkS0Gj'
>>> type(tmp)
<type 'str'>
>>> import os
>>> os.remove(tmp)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 2] No such file or directory: '/tmp/tmpZkS0Gj'




You might like to read help(tempfile.mktemp).

(By the way... the whole point of using tempfile is to avoid needing to
delete the file by hand afterwards.)



--
Steven

Shane Geiger

12/28/2007 4:50:00 AM

0

Yes, I knew this. Good call, it was just a bad copy and paste example
of lines that showed up close together in a file. My apologies.

>> import tempfile
>> tmp = tempfile.mktemp()
>>
>> import os
>> os.remove(tmp)
>>
>
> Not only does that not answer the Original Poster's question, but I don't
> think it does what you seem to think it does.
>
>
>
>>>> tmp = tempfile.mktemp()
>>>> tmp
>>>>
> '/tmp/tmpZkS0Gj'
>
>>>> type(tmp)
>>>>
> <type 'str'>
>
>>>> import os
>>>> os.remove(tmp)
>>>>
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> OSError: [Errno 2] No such file or directory: '/tmp/tmpZkS0Gj'
>
>
>
>
> You might like to read help(tempfile.mktemp).
>
> (By the way... the whole point of using tempfile is to avoid needing to
> delete the file by hand afterwards.)
>
>
>
>


--
Shane Geiger
IT Director
National Council on Economic Education
sgeiger@ncee.net | 402-438-8958 | http://ww...

Leading the Campaign for Economic and Financial Literacy

kar1107@gmail.com

12/28/2007 8:15:00 PM

0

On Dec 27, 7:36 pm, Steven D'Aprano
<ste...@REMOVE.THIS.cybersource.com.au> wrote:
> On Thu, 27 Dec 2007 21:17:01 -0600, Shane Geiger wrote:
> > import tempfile
> > tmp = tempfile.mktemp()
>
> > import os
> > os.remove(tmp)
>
> Not only does that not answer the Original Poster's question, but I don't
> think it does what you seem to think it does.
>
> >>> tmp = tempfile.mktemp()
> >>> tmp
> '/tmp/tmpZkS0Gj'
> >>> type(tmp)
> <type 'str'>
> >>> import os
> >>> os.remove(tmp)
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> OSError: [Errno 2] No such file or directory: '/tmp/tmpZkS0Gj'
>
> You might like to read help(tempfile.mktemp).
>
> (By the way... the whole point of using tempfile is to avoid needing to
> delete the file by hand afterwards.)

FWIW tempfile.mkstemp needs explicit user deletion. And
tempfile.mkstemp is recommended over tempfile.mktemp due to security
reasons.

Help on function mkstemp in module tempfile:

mkstemp(suffix='', prefix='tmp', dir=None, text=False)
mkstemp([suffix, [prefix, [dir, [text]]]])
User-callable function to create and return a unique temporary
file. The return value is a pair (fd, name) where fd is the
file descriptor returned by os.open, and name is the filename.

If 'suffix' is specified, the file name will end with that suffix,
otherwise there will be no suffix.

If 'prefix' is specified, the file name will begin with that
prefix,
otherwise a default prefix is used.

If 'dir' is specified, the file will be created in that directory,
otherwise a default directory is used.

If 'text' is specified and true, the file is opened in text
mode. Else (the default) the file is opened in binary mode. On
some operating systems, this makes no difference.

The file is readable and writable only by the creating user ID.
If the operating system uses permission bits to indicate whether a
file is executable, the file is executable by no one. The file
descriptor is not inherited by children of this process.

Caller is responsible for deleting the file when done with it.
<-------

>>>


Karthik

>
> --
> Steven