Understanding tempfile.TemporaryFile

Discussion in 'Python' started by byte8bits@gmail.com, Dec 28, 2007.

  1. Guest

    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
    , Dec 28, 2007
    #1
    1. Advertising

  2. John Machin Guest

    On Dec 28, 1:49 pm, 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.
    John Machin, Dec 28, 2007
    #2
    1. Advertising

  3. Shane Geiger Guest

    import tempfile
    tmp = tempfile.mktemp()

    import os
    os.remove(tmp)



    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
    | 402-438-8958 | http://www.ncee.net

    Leading the Campaign for Economic and Financial Literacy
    Shane Geiger, Dec 28, 2007
    #3
  4. 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
    Steven D'Aprano, Dec 28, 2007
    #4
  5. Guest

    On Dec 27, 10:12 pm, John Machin <> 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
    , Dec 28, 2007
    #5
  6. 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
    Steven D'Aprano, Dec 28, 2007
    #6
  7. Shane Geiger Guest

    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
    | 402-438-8958 | http://www.ncee.net

    Leading the Campaign for Economic and Financial Literacy
    Shane Geiger, Dec 28, 2007
    #7
  8. On Dec 27, 7:36 pm, Steven D'Aprano
    <> 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
    Karthik Gurusamy, Dec 28, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brad
    Replies:
    0
    Views:
    278
  2. Replies:
    2
    Views:
    338
  3. John Nagle
    Replies:
    2
    Views:
    932
    Carl Banks
    Nov 12, 2010
  4. Replies:
    11
    Views:
    131
  5. Alex van der Spek

    Difference between tempfile and spooled tempfile?

    Alex van der Spek, Apr 5, 2012, in forum: Python
    Replies:
    2
    Views:
    343
    Steve Howell
    Apr 5, 2012
Loading...

Share This Page